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)
#1
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
Reply
#2
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.
Reply
#3
The data in wolfram and Wikipedia is more accurate.
where can i find who are the owners of the improper routine.
Reply
#4
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.
Reply
#5
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.
Reply
#6
>>> math.log(2)
0.6931471805599453
when printed it's rounded to correct value
Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [CPython] Can't run script python VietVH 2 1,164 May-21-2022, 04:49 PM
Last Post: VietVH
  ImportError: /home/ali/Documents/test/charm/core/crypto/AES.cpython-39-x86_64-linux-g Anldra12 0 1,209 Apr-28-2022, 07:13 AM
Last Post: Anldra12
  Reducing runtime memory usage in Cpython interpreter david_the_graower 2 2,174 Oct-18-2021, 09:56 PM
Last Post: david_the_graower
  Navigating cpython source using python quazirfan 3 1,985 Aug-09-2021, 07:52 PM
Last Post: quazirfan
  IronPython from CPython amasis 3 2,156 Feb-05-2021, 12:26 PM
Last Post: jefsummers
  ImportError: /home/pybind11_example.cpython-37m-x86_64-linux-gnu.so: undefined symbol chaitra 2 5,026 Feb-03-2021, 05:14 AM
Last Post: chaitra
  compiled cpython output wrong version bigrockcrasher 0 1,478 Feb-25-2020, 06:31 PM
Last Post: bigrockcrasher
  CPython: Knowing when the interpreter expects more input b0bh00d 1 2,064 Apr-18-2019, 05:08 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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