Package lib :: Module mathematics
[hide private]
[frames] | no frames]

Source Code for Module lib.mathematics

 1  ############################################################################### 
 2  #                                                                             # 
 3  # Copyright (C) 2013 Edward d'Auvergne                                        # 
 4  #                                                                             # 
 5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
 6  #                                                                             # 
 7  # This program is free software: you can redistribute it and/or modify        # 
 8  # it under the terms of the GNU General Public License as published by        # 
 9  # the Free Software Foundation, either version 3 of the License, or           # 
10  # (at your option) any later version.                                         # 
11  #                                                                             # 
12  # This program is distributed in the hope that it will be useful,             # 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
15  # GNU General Public License for more details.                                # 
16  #                                                                             # 
17  # You should have received a copy of the GNU General Public License           # 
18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
19  #                                                                             # 
20  ############################################################################### 
21   
22  # Module docstring. 
23  """Module for basic mathematical operations.""" 
24   
25  # Python module imports. 
26  from math import ceil, log10 
27   
28   
29 -def order_of_magnitude(value):
30 """Determine the order of magnitude of the given number. 31 32 For example, the number 1,234 will be give a value of 4.0. 33 34 35 @param value: The value to determine the order of magnitude of. 36 @type value: float or int 37 @return: The order of magnitude. 38 @rtype: float 39 """ 40 41 # Calculate and return the value. 42 return ceil(log10(value))
43 44
45 -def round_to_next_order(value):
46 """Round the given value up to the next order of magnitude. 47 48 For example, the number 1,234 will be rounded up to 10,000. 49 50 51 @param value: The value to determine the order of magnitude of. 52 @type value: float or int 53 @return: The new value rounded up to the next order of magnitude. 54 @rtype: float 55 """ 56 57 # Calculate and return the value. 58 return 10**(order_of_magnitude(value))
59