0

I'm currently writing a program that checks for the multiplicity of a number by a factor of 10. However, this program is violated by what I assume is a double-precision error in python. I have attached an example below.

# correct representation in line with expectation
>> 71885 / 71.885 = 1000.0

# this fails, when we should be reporting 10^6 (1000000)
>> 310888679 / 310.888679 = 999999.99999999
martineau
  • 119,623
  • 25
  • 170
  • 301
Will_E
  • 68
  • 9
  • 3
    It's not a strict duplicate, but this is basically a different question with the answer [yep, floating point math is "broken"](https://stackoverflow.com/q/588004/364696). This isn't specific to Python, basically everyone agrees on a definition of binary floating point that can't accurately represent many simple decimal values. `310.888679` is actually a value very slightly larger than that (`310.888679000000024643668439239…`), so when you divide using it, the result is ever so slightly smaller than expected. – ShadowRanger Feb 24 '21 at 23:32
  • 1
    use math.isclose() - – Tony Suffolk 66 Feb 24 '21 at 23:36
  • I'll have to work around this issue it seems. I won't be able to math.isclose() in my context, unfortunately. – Will_E Feb 24 '21 at 23:41
  • I thought I had a good answer to this, but it's actually _still_ bitten by IEEE 754 floating point math See also https://stackoverflow.com/a/64902818/4541045 – ti7 Feb 24 '21 at 23:53
  • 1
    Totally agree it's a dup of the linked question, FWIW. – martineau Feb 25 '21 at 00:27

0 Answers0