Python Forum
ln2 value is _math.c (cpython)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ln2 value is _math.c (cpython)
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:
it is from Sun micro-systems.
Contact 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)
>>> y = 6.93147180559945286227E-01
>>> y == log(2)
so there is no difference in ieee754 representation on 64 bits.
>>> math.log(2)
when printed it's rounded to correct value
You can get more decimals with the decimal module
>>> Decimal.from_float(log(2))
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.

Possibly Related Threads…
Thread Author Replies Views Last Post
  compiled cpython output wrong version bigrockcrasher 0 310 Feb-25-2020, 06:31 PM
Last Post: bigrockcrasher
  CPython: Knowing when the interpreter expects more input b0bh00d 1 564 Apr-18-2019, 05:08 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020