0

I want to know how to make floating-point equations correctly in Python3.

I am trying to solve a linear equation problem and have variables and statements below.

slope = float(70 / 23)
c = 9
(slope * -161) + c == -481 # False
print((slope * -161) + c) # -481.00000000000006

If you manually evaluate (slope * -161) + c, you will get -481. However, python evaluates it as -481.00000000000006 when I use float. How do I resolve this issue?

Changnam Hong
  • 1,669
  • 18
  • 29
  • 1
    How did you "manually evaluate" it? When I divide 70 by 23 I get 3.043478260869565, and when I multiply it by -161 I get -489.999999999999965, and when I add 9 I get -480.999999999999965. Unless you evaluate it with infinite precision, or using rational numbers, you're just about always going to get an answer that's ever-so-slightly off, so if you expect a "nice" answer you may need to round it. (Perhaps when you "manually" evaluated it you used a calculator that rounded for you.) – Steve Summit Jan 08 '23 at 13:44
  • Your question was closed as a duplicate of [that question](https://stackoverflow.com/questions/588004/is-floating-point-math-broken), although that one focuses on a different issue. As I've shown, for this problem you get a "wrong" result regardless of whether you use decimal or binary, so it's not really a binary/decimal problem. – Steve Summit Jan 08 '23 at 13:49
  • 1
    @MarkDickinson I was trying to demonstrate "manual" arithmetic, not IEEE-754. Usually when people think that IEEE-754 has given a "wrong" result, it's because their idea of the "right" result derives from a decimal evaluation. I was trying to demonstrate that, for this problem, decimal doesn't necessarily give the "right" answer, either. So I used bc, with scale=15. (bc is fixed-point decimal, not floating-point binary.) – Steve Summit Jan 09 '23 at 15:45
  • But then I tried the MacOS calculator, and it gave a final result of -481.000, exactly. Ironically, if you do the same problem in IEEE-754 *single* precision, you get -481.000 exactly also. So when I said "Unless you evaluate it with infinite precision ... you're just about always going to get an answer that's ever-so-slightly off" I might have been wrong — maybe it's more like half the time. – Steve Summit Jan 09 '23 at 15:45
  • @SteveSummit: Yes, apologies; I realised that just after I'd posted my comment, then deleted the comment in the hope that no-one had noticed my foolishness. Too late, apparently. – Mark Dickinson Jan 09 '23 at 18:40
  • @MarkDickinson No worries, the extra explanations I wrote aren't entirely amiss (and you prompted me to investigate the behavior with single-precision, which was eye-opening). – Steve Summit Jan 09 '23 at 18:57
  • @SteveSummit, I missed your comment. Getting the result of `-481` manually, means the value calculated by writing a formula on paper. Since `23 * -7 = 161` is true mathematically, substituting the values ​​for each variable of `(slope * -161) + c` results in `-481`. As you mentioned I resolved this issue by rounding using [math.isclose()](https://docs.python.org/3/library/math.html#math.isclose) – Changnam Hong Jan 10 '23 at 04:57

0 Answers0