Download code
From LiteratePrograms
Back to Logarithm_Function_(Python)
Download for Windows: zip
Download for UNIX: zip, tar.gz, tar.bz2
logN.py
1 #!/usr/bin/python 2 # Copyright (c) 2010 the authors listed at the following URL, and/or 3 # the authors of referenced articles or incorporated external code: 4 # http://en.literateprograms.org/Logarithm_Function_(Python)?action=history&offset=20070217193512 5 # 6 # Permission is hereby granted, free of charge, to any person obtaining 7 # a copy of this software and associated documentation files (the 8 # "Software"), to deal in the Software without restriction, including 9 # without limitation the rights to use, copy, modify, merge, publish, 10 # distribute, sublicense, and/or sell copies of the Software, and to 11 # permit persons to whom the Software is furnished to do so, subject to 12 # the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be 15 # included in all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 # 25 # Retrieved from: http://en.literateprograms.org/Logarithm_Function_(Python)?oldid=9156 26 27 28 from __future__ import division 29 import math 30 31 def logN(X, base=math.e, epsilon=1e-12): 32 # logN is logarithm function with the default base of e 33 integer = 0 34 if X < 1 and base < 1: 35 raise ValueError, "logarithm cannot compute" 36 while X < 1: 37 integer -= 1 38 X *= base 39 while X >= base: 40 integer += 1 41 X /= base 42 partial = 0.5 # partial = 1/2 43 # list = [] # Prepare an empty list, it seems useless 44 X *= X # We perform a squaring 45 decimal = 0.0 46 while partial > epsilon: 47 if X >= base: # If X >= base then a_k is 1 48 decimal += partial # Insert partial to the front of the list 49 X = X / base # Since a_k is 1, we divide the number by the base 50 partial *= 0.5 # partial = partial / 2 51 X *= X # We perform the squaring again 52 return (integer + decimal) 53 54 if __name__ == '__main__': 55 value = 4.5 56 print " X = ", value 57 print " ln(X) = ", logN(value) 58 print " log4(X) = ", logN(value, base=4) 59
logNdp.py
1 #!/usr/bin/python 2 # Copyright (c) 2010 the authors listed at the following URL, and/or 3 # the authors of referenced articles or incorporated external code: 4 # http://en.literateprograms.org/Logarithm_Function_(Python)?action=history&offset=20070217193512 5 # 6 # Permission is hereby granted, free of charge, to any person obtaining 7 # a copy of this software and associated documentation files (the 8 # "Software"), to deal in the Software without restriction, including 9 # without limitation the rights to use, copy, modify, merge, publish, 10 # distribute, sublicense, and/or sell copies of the Software, and to 11 # permit persons to whom the Software is furnished to do so, subject to 12 # the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be 15 # included in all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 # 25 # Retrieved from: http://en.literateprograms.org/Logarithm_Function_(Python)?oldid=9156 26 27 28 from __future__ import division 29 import math 30 31 # Note: compute the log of X>=1. Doesn't work for X<1. Ex: logN(0.5) returns -1.698970... instead of -0.301029... 32 33 def logN(X, base=math.e, decimalplaces=12): 34 integer = 0 35 while X < 1: 36 integer -= 1 37 X *= base 38 while X >= base: 39 integer += 1 40 X /= base 41 result = str(integer) + '.' 42 while decimalplaces > 0: 43 X = X ** 10 # Calc X to the 10th power 44 digit = 0 45 while X >= base: 46 digit += 1 47 X /= base 48 result += str(digit) 49 decimalplaces -= 1 50 return result 51 52 53 if __name__ == '__main__': 54 value = 4.5 55 print " X = ", value 56 print " 3 decimal places LOG(X) = ", logN(value, base=10, decimalplaces=3) 57 print " 6 decimal places LOG(X) = ", logN(value, base=10, decimalplaces=6) 58 print "12 decimal places LOG(X) = ", logN(value, base=10) 59
