Logarithms

suggest change

math.log(x) gives the natural (base e) logarithm of x.

math.log(math.e)  # 1.0
math.log(1)       # 0.0
math.log(100)     # 4.605170185988092

math.log can lose precision with numbers close to 1, due to the limitations of floating-point numbers. In order to accurately calculate logs close to 1, use math.log1p, which evaluates the natural logarithm of 1 plus the argument:

math.log(1 + 1e-20)  # 0.0
math.log1p(1e-20)    # 1e-20

math.log10 can be used for logs base 10:

math.log10(10)  # 1.0

When used with two arguments, math.log(x, base) gives the logarithm of x in the given base (i.e. log(x) / log(base).

math.log(100, 10) # 2.0
math.log(27, 3)   # 3.0
math.log(1, 10)   # 0.0

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Math module:
* Logarithms

Table Of Contents
2 Filter
3 List
6 Math module
7 Loops
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve