0

I am using float values in my program, but for some reason the addition of two float values sometimes gives a very strange result

For example:

print(float(3.1) + float(18.8))

output: 21.900000000000002

Why does this happen, and how can I fix it?

MatthewG
  • 796
  • 1
  • 4
  • 21
  • 1
    You're not showing us everything here: that output is very unlikely in a normal setup. I strongly suspect that you or something in your system has done the equivalent of `float = Decimal`. (Which, incidentally, I'd advise strongly against.) BTW, "Is floating point math broken" is *not* a good duplicate here. – Mark Dickinson May 30 '19 at 07:53
  • 1
    @MarkDickinson Please elaborate why the dupe is not a good dupe? – kvantour May 30 '19 at 10:19
  • 2
    @kvantour looked like a good dupe to me, good enough for me anyway – MatthewG May 30 '19 at 10:47
  • 2
    @kvantour There's much more going on here than in the usual "floating-point is imprecise" situation. What's actually happening in this question is that the name `float`, which usually refers to the built-in Python binary floating-point type, has been somehow rebound to refer to the `Decimal` type from the std. lib. instead. With normal floating-point imprecisions and Python's usual floating-point printing, you'd never see such a long output, or so many trailing nonzero digits. The second marked duplicate is a better one: the main issue here is the use of `float(3.1)` instead of `float("3.1")`. – Mark Dickinson May 30 '19 at 11:33
  • 2
    But it's far from clear that the second duplicate is even relevant _until_ you realise that the name `float` has been rebound. None of the answers in the first dupe would lead the OP to the `float("3.1")` solution. (Though the real solution is to figure out how on earth that rebinding from `float` to `Decimal` is happening in the first place, and stop it. :-) – Mark Dickinson May 30 '19 at 11:34
  • @kvantour: Suppose there were a question that asked “Is C broken?” and its answers explained that C has many rules, and writing various programs results in the rules causing the programs to produce incorrect output. And maybe the answers point to diverse sources, such as the C standard and some primers, textbooks, and academic articles. And then, anytime anybody asked a C question, it was marked as a duplicate of “Is C broken?” Overrun an array: Duplicate of “Is C broken?” Return a pointer to an automatic object? Duplicate of “Is C broken?” Modify a string literal? Dup. Alias an object? Dup. – Eric Postpischil May 30 '19 at 11:45
  • @kvantour: So that is what is wrong with promiscuously marking floating-point questions as duplicates of “Is floating point math broken?” Language formatting rules make a number appear more accurate than it is? Duplicate. A calculation sequence gets more error than the OP expected? Duplicate. OP requests a means for more accurate calculation? Duplicate. Sometimes these promiscuous closures as duplicates are just not helpful because the “original” does not give information particularly relevant to the specific question, and sometimes they are just wrong. – Eric Postpischil May 30 '19 at 11:46
  • @EricPostpischil I fully agree with both your analysis here. I believe it even should go to meta. If this question would be reopened, it would be nice to see a dedicated answer here explaining these differences in detail. Just to be clear, I asked to elaborate on the duplicate, just because the sentence, _"Is floating point math broken" is not a good duplicate here._ did not give me enough information to validate that statement. So the _discussion_ that appeared here, was exactly what I was after. – kvantour May 30 '19 at 12:24
  • @MarkDickinson Just to nudge on you on the previous comment I passed to ErikPostpiscil – kvantour May 30 '19 at 12:24
  • @MarkDickinson: FYI, somebody (not me) reopened this. You should enter an answer noting that `float` was rebound to `Decimal`. – Eric Postpischil May 30 '19 at 13:14
  • Wow this has blown up, sorry guys this is **my fault**. I tried to fix my problem by changing using the `Decimal` class, before I came to ask my question, then I accidentally posted the output for when I was using the `Decimal` class but was asking about `float` values. Sorry guys my fault, I've updated the output in the question to reflect this. – MatthewG May 30 '19 at 13:39
  • Even if `float = Decimal` was used, the original example was *still* converting `float`s (not `str`s, due to the use of floating-point literals) to `Decimal`. The only difference is the number of digits shown by the `print`. – chepner May 30 '19 at 13:44
  • Just want to apologize for wasting everyone's time on a problem that didn't exist. I got the answer I was looking for, so then went and continued working on my task, and wasn't paying attention to my stackoverflow. – MatthewG May 30 '19 at 13:45
  • 1
    @MarkDickinson Question is now edited to an MCVE (by OP), closing again. – wim May 30 '19 at 18:46

0 Answers0