Python Forum

Full Version: ln2 value is _math.c (cpython)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi. I'm not so sure where to ask it, so I'm trying here.
m looking at the source code of _math.c (line 25):

#if !defined(HAVE_ACOSH) || !defined(HAVE_ASINH)
static const double ln2 = 6.93147180559945286227E-01;
static const double two_pow_p28 = 268435456.0; /* 2**28 */
and I noticed that ln2 value is different from the what wolframalpha value for ln2. (bald part is the difference)

ln2 = 0.693147180559945286227 (cpython)

ln2 = 0.6931471805599453094172321214581 (wolframalpha)

ln2 = 0.693147180559945309417232121458 (wikipedia)

so my question is why there is a difference? what am I missing?


I asked this question also in Stack-overflow if it's matter - stackoverflow question
I would expect them to be the same.

Do you know which is more accurate?

Once that is known, you should report your findings to the
owners of the improper routine.
The data in wolfram and Wikipedia is more accurate.
where can i find who are the owners of the improper routine.
go to python github account: https://github.com/python/cpython/blob/m...es/_math.c
it is from Sun micro-systems.
Contact python.org and file bug report
Search bugs before making new post.
I think the answers you got from stackoverflow are correct. The hard coded value is not log(2) but log(2) truncated to the ieee754 representation on presumably 64 bits.

Concerning wolfram alpha's result here is what python says
>>> from math import log
>>> x = 0.6931471805599453094172321214581
>>> x == log(2)
True
>>> y = 6.93147180559945286227E-01
>>> y == log(2)
True
so there is no difference in ieee754 representation on 64 bits.
>>> math.log(2)
0.6931471805599453
when printed it's rounded to correct value
You can get more decimals with the decimal module
>>> Decimal.from_float(log(2))
Decimal('0.69314718055994528622676398299518041312694549560546875')
which shows that the hard-coded value was indeed the decimal value of the ieee-754 number rounded to a higher order. I agree that it is strange.