Issue32783
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) * ![]() |
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) * ![]() |
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) * ![]() |
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) * ![]() |
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) * ![]() |
Date: 2018-02-07 13:35 | |
Closing. There's no actual bug here. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:58:57 | admin | set | github: 76964 |
| 2018-02-07 13:35:29 | mark.dickinson | set | status: open -> closed resolution: not a bug messages: + msg311790 stage: needs patch -> resolved |
| 2018-02-07 12:35:21 | mark.dickinson | set | messages: + msg311789 |
| 2018-02-07 03:01:47 | tim.peters | set | messages: + msg311762 |
| 2018-02-07 00:14:57 | steven.daprano | set | nosy:
+ steven.daprano, tim.peters messages:
+ msg311755 |
| 2018-02-06 21:11:57 | christian.heimes | set | nosy:
+ christian.heimes messages: + msg311750 |
| 2018-02-06 21:00:51 | r.david.murray | set | nosy:
+ mark.dickinson |
| 2018-02-06 20:47:53 | matanya.stroh | create | |
