Bug in math.log function

math.log(536870912 , 2) gives 29.000000000000004 Whereas it should give 29.0

Le 6/18/21 à 11:40 PM, Abhinav Gupta a écrit :
math.log(536870912 , 2) gives 29.000000000000004 Whereas it should give 29.0
Thanks for reporting. Please note this is the documentation mailing list, to report issues about the documentation, not the implementation. Next time please use https://bugs.python.org to report Python bugs. The approximation you're getting is due to a limitation in the floating point number representation in your CPU [1], as your CPU probably use a limited number of bits to represent any given number [2]. You can use the Decimal module to achieve more precision if needed, circumventing the CPU limitation by "doing the math itself":
Which looks like 29 to me [3]. [1]: https://docs.python.org/3/tutorial/floatingpoint.html [2]: https://fr.wikipedia.org/wiki/IEEE_754 [3]: https://en.wikipedia.org/wiki/0.999... Hope it helps, -- [Julien Palard](https://mdk.fr)

Le 6/18/21 à 11:40 PM, Abhinav Gupta a écrit :
math.log(536870912 , 2) gives 29.000000000000004 Whereas it should give 29.0
Thanks for reporting. Please note this is the documentation mailing list, to report issues about the documentation, not the implementation. Next time please use https://bugs.python.org to report Python bugs. The approximation you're getting is due to a limitation in the floating point number representation in your CPU [1], as your CPU probably use a limited number of bits to represent any given number [2]. You can use the Decimal module to achieve more precision if needed, circumventing the CPU limitation by "doing the math itself":
Which looks like 29 to me [3]. [1]: https://docs.python.org/3/tutorial/floatingpoint.html [2]: https://fr.wikipedia.org/wiki/IEEE_754 [3]: https://en.wikipedia.org/wiki/0.999... Hope it helps, -- [Julien Palard](https://mdk.fr)
participants (2)
-
Abhinav Gupta
-
Julien Palard