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: math.log() returns improper output
Type: behavior Stage: resolved
Components: Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: joshinsel, mark.dickinson
Priority: normal Keywords:

Created on 2021-12-21 06:59 by joshinsel, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg408986 - (view) Author: Joshua Insel (joshinsel) Date: 2021-12-21 06:59
math.log(536870912, 2) should return 29.0, because 2^29 = 536870912. However, it instead returns 29.000000000000004.
msg408988 - (view) Author: Joshua Insel (joshinsel) Date: 2021-12-21 07:20
The same problem occurs with the log function from the C standard library, which Python uses to implement its same-named function.
msg408992 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-12-21 08:54
Yes, confirmed that this is not a bug, but just one of the many consequences of approximating real numbers by floating-point numbers.

You may be interested in math.log2 and/or int.bit_length. math.log2(x) *may*  give you more accurate results than math.log(x, 2) when x is a power of two, but there are no guarantees - we're at the mercy of the C math library here.
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90302
2021-12-21 08:54:04mark.dickinsonsetresolution: not a bug

messages: + msg408992
nosy: + mark.dickinson
2021-12-21 07:20:48joshinselsetstatus: open -> closed

messages: + msg408988
stage: resolved
2021-12-21 06:59:34joshinselcreate