Python Forum
ln2 value is _math.c (cpython) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: ln2 value is _math.c (cpython) (/thread-8113.html)



ln2 value is _math.c (cpython) - matanya_stroh - Feb-06-2018

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


RE: ln2 value is _math.c (cpython) - Larz60+ - Feb-06-2018

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.


RE: ln2 value is _math.c (cpython) - matanya_stroh - Feb-06-2018

The data in wolfram and Wikipedia is more accurate.
where can i find who are the owners of the improper routine.


RE: ln2 value is _math.c (cpython) - Larz60+ - Feb-06-2018

go to python github account: https://github.com/python/cpython/blob/master/Modules/_math.c
it is from Sun micro-systems.
Contact python.org and file bug report
Search bugs before making new post.


RE: ln2 value is _math.c (cpython) - Gribouillis - Feb-06-2018

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.


RE: ln2 value is _math.c (cpython) - buran - Feb-06-2018

>>> math.log(2)
0.6931471805599453
when printed it's rounded to correct value


RE: ln2 value is _math.c (cpython) - Gribouillis - Feb-06-2018

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.