Download code

Jump to: navigation, search

Back to Logarithm_Function_(Python,_functional)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

alog10.py

 1 # The authors of this work have released all rights to it and placed it
 2 # in the public domain under the Creative Commons CC0 1.0 waiver
 3 # (http://creativecommons.org/publicdomain/zero/1.0/).
 4 # 
 5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 # 
13 # Retrieved from: http://en.literateprograms.org/Logarithm_Function_(Python,_functional)?oldid=8959
14 
15 fix   = lambda f,v1,v0=None: (v0 == v1) and v0 or fix(f, f(*v1), v1)
16 shift = lambda x,l,s: (x/(10.0**s), l+s)
17 scale = lambda x,l: shift(x, l, (x >= 10)-(x < 1))
18 ipart = lambda x: fix(scale, (x,0))
19 
20 digit = lambda x,l,d: l + alog(x**10, d-1)/10.0
21 alog  = lambda x,d=12: (d>=0) and digit(*ipart(x)+(d,)) or 0.0
22 if __name__ == "__main__":
23     value = 4.5
24     print alog(value, 3)
25     print alog(value, 6)
26     print alog(value)
27     e = 2.7182818284590451
28     print alog(10**e)
29     print 10**alog(e)
30     print e


hijacker
hijacker
hijacker
hijacker