This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ln(2) isn't accurate in _math.c in cpython
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, mark.dickinson, matanya.stroh, steven.daprano, tim.peters
Priority: normal Keywords:

Created on 2018-02-06 20:47 by matanya.stroh, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg311749 - (view) Author: Matanya Stroh (matanya.stroh) Date: 2018-02-06 20:47
In cpython/Modules/_math.c there is definition of as const ln2
the value that in that const isn't correct (at least not accurate)

This is value that in the file:

ln2 = 0.693147180559945286227 (cpython)

but when calculating the value in wolframalpha, this is the value we get.
ln2 = 0.6931471805599453094172321214581 (wolframalpha)

and this is the value from Wikipedia
ln2 = 0.693147180559945309417232121458 (wikipedia)

also this is the thread in stackoverflow regarding this issue
https://stackoverflow.com/questions/48644767/ln2-const-value-in-math-c-in-cpython
msg311750 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2018-02-06 21:11
Welcome to the astounding world of IEEE 754 floating point operations. Python's float type is based on C's 64bit double precision float. The value in _math.c is as precise as a double is able to contain:

>>> 0.6931471805599453094172321214581 == 0.693147180559945286227
True
>>> 0.6931471805599453094172321214581
0.6931471805599453
>>> 0.693147180559945286227
0.6931471805599453

Strictly speaking any of the numbers in your initial message are wrong. It's not possible to express ln2 is a number of finite length. The approximations are usually good enough, though.
msg311755 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2018-02-07 00:14
Thanks for the link to the Stackoverflow discussion. Currently there are three answers: two (correctly) answer that the constant as given is the most accurate value for IEEE-754 floats.

Tim Peters has answered that: https://stackoverflow.com/a/48653387

"Either way, the code ensures the best 53-bit approximation to log(2) will be used."

so there is no other value that ln2 can be given that is more accurate given the limitation of 53-bits.

https://stackoverflow.com/a/48653387

The difference between the given value and the mathematically precise value is not a bug but intentional. I'm not closing this ticket as I think that this should be documented in the source, to prevent future confusion.
msg311762 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2018-02-07 03:01
As I mentioned on StackOverflow, the literal in question appears to have been crafted to convert to the best possible 53-bit binary approximation to log(2) regardless of whether a compiler converts it to double precision (53 bits of precision) or to "double extended" (64 bits of precision).  In the latter case, the literal is such that the "extra" trailing 11 bits are all zeroes.

Since I doubt that's a potential issue anymore, even trying to document it would be confusing too ;-)

So while I'll defer to Mark, I'm inclined to just close this as "not a bug".  Math libraries typically bristle with code that's baffling to the uninitiated, and comments about things that don't matter can get in the way.  The only thing a libm developer cares about here is whether or not the literal converts to the best double approximation to log(2) - and it does.
msg311789 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2018-02-07 12:35
> I'm inclined to just close this as "not a bug".

Sounds good to me.

The ideal would probably be to use a hex literal here. They're part of C99 (see section 6.4.4.2), but last time I checked Visual Studio didn't support them. I don't know whether that's changed recently.
msg311790 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2018-02-07 13:35
Closing. There's no actual bug here.
History
Date User Action Args
2022-04-11 14:58:57adminsetgithub: 76964
2018-02-07 13:35:29mark.dickinsonsetstatus: open -> closed
resolution: not a bug
messages: + msg311790

stage: needs patch -> resolved
2018-02-07 12:35:21mark.dickinsonsetmessages: + msg311789
2018-02-07 03:01:47tim.peterssetmessages: + msg311762
2018-02-07 00:14:57steven.dapranosetnosy: + steven.daprano, tim.peters

messages: + msg311755
stage: needs patch
2018-02-06 21:11:57christian.heimessetnosy: + christian.heimes
messages: + msg311750
2018-02-06 21:00:51r.david.murraysetnosy: + mark.dickinson
2018-02-06 20:47:53matanya.strohcreate