I'm trying to check in Python if a math.log(x, base) has decimal or not:
import math
# math.log(8, 2) = 3
print math.log(8)
print math.log(2)
print math.log(8) / math.log(2)
print 2.07944154168 % 0.69314718056
print math.log(8) % math.log(2)
Output is:
2.07944154168
0.69314718056
3.0
0.0
0.69314718056
Why does fourth print row return zero but fifth does not?
This will probably be closed for being a duplicate, but just so one can see how this plays out:
>>> import math
>>> math.log(8)
2.0794415416798357
>>> math.log(2)
0.6931471805599453
Now let's say you need to compute math.log(8) % math.log(2). You need to compute the remainder after dividing math.log(2) into math.log(8). Let's see, does it go in 3 times?
0.6931471805599453
+ 0.6931471805599453
+ 0.6931471805599453
--------------------
2.0794415416798359
Woah! We overshot the value of 2.0794415416798357 which means it actually goes in TWO times:
0.6931471805599453
+ 0.6931471805599453
--------------------
1.3862943611198906
Okay so what is the remainder?
2.0794415416798359
- 1.3862943611198906
--------------------
0.6931471805599453
So TL;DR your remainder is close to math.log(2) because of rounding errors. It does not go in exactly three times. It goes in two times with just about math.log(2) left over.
Yes when you print the quotient it says 3.0 but again, this is all rounding error in floating point, which is not unique to Python.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With