For years I’ve followed, through RSS, floating-point related questions on stackoverflow.com. Every day it seems there is a question like “why does 19.24 plus 6.95 equal 26.189999999999998?” I decided to track these questions, to see if my sense of their frequency was correct. I found that, in the last 40 days, there were 18 such questions. That’s not one per day, but still — a lot!

What intrigues me is that the questioners know they are using floating-point — they know enough to tag their questions as such — yet they lack the most basic understanding of it. And they’re programmer enough to know to use Stack Overflow. (Why they don’t search before asking I’ll never know.) If you just sat down and typed numbers into your first program, I would understand. But I don’t think all of these questions are from newbies.

But I’m not here to pontificate on the state of computer science education; I just wanted to document how often these questions are asked. I’ll show you excerpts (taken verbatim) from the 18 questions I saw. The questions came from the context of a variety languages: Java, C++, C#, Python, Javascript, awk, Perl, Objective-C, and PHP. If you read nothing else, read the titles of the questions — they pretty much sum it up.

(I wonder: how many of these questions are being asked in forums I *don’t* monitor?)

## 9/29/13: print double number in java and get wrong answer

double check =0.615 * 255 -0.515 * 255 -0.100 * 255; System.out.println(check);

*why get -2.8421709430404007E-14? it should be 0.0*

## 9/30/13: Floating point addition – giving strange result..!

float f = 0.1f; for(int i = 0; i<9; i++) { f += 0.1f; } System.out.println(f);

*The following output is displayed:*

*1.0000001*

*But output should be 1.0000000, right?*

## 10/16/13: Doubles don’t subtract decimals correctly

*… if I for example start with 1 dollar, then 0.23 dollars and lose, then bet 0.55 dollars and lose, and then bet 0.07 dollars and lose again, I can’t bet 0.15 dollars, even though the program claims I actually have 0.15 dollars … It appears the program is incorrectly substracting. However, I still can bet 0.149 dollars.*

## 10/17/13: Why does 1.2 – 0.1 become 1.09 in C#

*Why does this statement evaluate to 1.09 and not 1.1?*

## 10/18/13: Unexpected result of product of 2 double variables

double price = 4.35; double quantity = 100; double total = price * quantity; // Should be 100 * 4.35 = 435.00 System.out.println(total); // Prints 434.99999999999999

*Why does this happen?*

(Rick Regan: This is the same example from the Java Udacity class I wrote about; I wonder if this was prompted from the same class.)

## 10/21/13: Python math accuracy

*I have try this simple math operation in python*

>>> math.floor(8.2-0.21) 7.0 >>> math.floor(8.21-0.2) 8.0 >>> math.floor(8.2-0.2) 7.0

*The third one should return 8 but it is return 7?!*

## 10/24/13: Floating point addition in Python

*Why exactly does the latter case in Python doesn’t yield the result 3.3?*

>>> 1.0 + 2.3 3.3 >>> 1.1 + 2.2 3.3000000000000003

*It doesn’t seem to make any sense to me what is going on here. What are the limitations here for the representation of the same result that you are getting through 1.0 + 2.3 but not through 1.1 + 2.2?*

## 10/24/13: Simple addition exposes Javascript engine floating point error

*… stumble upon a really strange (at least as i occurs to me) behaviour exposed in the javascript engines.*

*No matter if entered into the console or performed inside a script, this is what happens:*

-1 + 0.85 --> -0.85 -1 + 1 --> 0 -1 + 1 + -.15 --> 0.15 -1 + 1.15 --> 0.1499999999999999

## 10/27/13: odd results with double precision in Java

*I’m simply trying to calculate percentage_imp, but instead of 0.22 (exactly 0.22, no rounding error), I get 0.22000000000000003!!*

## 10/27/13: Why this four lines code gives 0.45<0.45 is True

x=0.35 while (x<0.45): x=x+0.05 print x,"<",0.45, x<0.45

*below is the output:*

0.4 < 0.45 True 0.45 < 0.45 True 0.5 < 0.45 False

*Why 0.45<0.45 is true?*

## 10/29/13: floating point calculations in awk

*I am surprised with behaviour of awk while performing floating point calculations. It lead me to wrong calculation on table data.*

$ awk 'BEGIN {print 2.3/0.1}' 23 <-- Ok $ awk 'BEGIN {print int(2.3/0.1)}' 22 <-- Wrong! $ awk 'BEGIN {print 2.3-2.2==0.1}' 0 <-- Surprise! $ awk 'BEGIN {print (2.3-2.2>0.1)}' 0 <-- Wrong! $ awk 'BEGIN {print 2.3-2.2<0.1}' 1 <-- Totally confused now ...

(Rick Regan: I edited the output based on the appended correction to it.)

## 10/29/13: Simple Perl For-Loop

*I have a for loop and I want to increment the variable by 0.1 each time, however the value changes differently from the increment and I am unsure as to why.*

for (my $t = 1000; $t < 1500 ;$t+=0.1) { print "$t\n"; }

*It prints:*

1034.9 1035 1035.1 1035.2 1035.3 1035.4 1035.49999999999 1035.59999999999 1035.69999999999 1035.79999999999 1035.89999999999 1035.99999999999 1036.09999999999 1036.19999999999 1036.29999999999

[it then goes on like this to 1500]

*I do not know where the decimal places are coming from.*

## 10/30/13: issue with a number float

*…when i have this number and i try to print it i don’t give me back the same number:*

float myNumber = 27589353.0f; NSLog(@"My Number is %.2f", myNumber);

*the result is: My Number is 27589352.00*

## 10/31/13: Python float division “rounding error” on division by 100

*When dividing a float by 100 in Python 2.7 I get the following “rounding behaviour”:*

>>> 3.7e-03/100 3.7000000000000005e-05

*I would expect the following:*

>>> 3.7e-03/100 3.7e-05

## 10/31/13: Invalid value when using floats

*The following operation give out the wrong result.*

$First = '45.4000'; $Second = '4.6800000000'; $Third = '50.00'; echo ( $First + $Second ) - $Third;

*OUTPUT: 0.079999999999998*

*Expected Output: 0.08*

## 11/6/13: PHP math gets crazy, need explanation, my brain is melting

$val = 1232.81; $p1 = 1217.16; $p2 = 15.65; $sum = $p1 + $p2; $dif = $val - $sum; echo $dif;

*It will give you -2.2737367544323E-13 … yeah, ALMOST zero, but then why it doesn’t say 0 ?*

(Rick Regan: That is my favorite title.)

## 11/6/13: Double randomly adds 0.000000000000001

*…lower ticks once every 100 milliseconds. When it:*

Gets to 8

Gets to 5

Gets to 1

*it increases by 0.000000000000001. Why?*

## 11/7/13: In Javascript: 1.1 – 1 = 0.10000000000000009

*What kind of mistakes? How do I fix this?*

*1.1 – 1 = 0.10000000000000009*

*1.0000001+1 = 2.0000001000000003*

## Looking For Answers?

If you came here looking for answers, these articles of mine may help:

- The Four Stages of Floating-Point Competence.
- Floating-Point Is So Insane Even a Ten-Year Old Can See It.
- Why 0.1 Does Not Exist In Floating-Point.
- The Answer is One (Unless You Use Floating-Point).
- When Doubles Don’t Behave Like Doubles.
- When Floats Don’t Behave Like Floats.

> the questioners know they are using floating-point

Yes, but that doesn’t mean they realize what that means. I imagine many programmers have been introduced to the concept of floating-point as simply “how you do math with decimals”. There’s a big gap from knowing int math isn’t sufficient and actually understanding what’s going on with floats. Certainly, they’d do well to search before posting, but everyone has to learn the intricacies of floating-point math somehow, and that’s much harder than simply using floats.

@Michael,

It would be interesting to know how it’s taught today (it’s been a very long time since I was taught it). I can’t imagine though that the person teaching it wouldn’t say — within a sentence or two of saying “floating-point” — that it “can’t represent all decimal numbers accurately”. It’s its most basic property; I don’t see it as one of its intricacies.

|Update 11/13/13: I wrote an article about what I found in my college notebooks.

My favorite stack overflow float question is from a month ago:

“0.1 float is greater than 0.1 double. I expected it to be false”

http://stackoverflow.com/questions/19292283/0-1-float-is-greater-than-0-1-double-i-expected-it-to-be-false?newsletter=1&nlcode=157256%7C3b1b

The irrational assumptions embodied in that question make me smile.

I haven’t tried monitoring stackoverflow. I just find out about these questions when I see new incoming links to my blog. The frequency of these questions is a bit discouraging, since they are mostly all variants on exactly the same question.

Bruce,

I saw that one but I didn’t include it on my list because the questioner indicated basic knowledge of floating-point (“As 0.1 cannot be perfectly represented in binary…”).

(BTW, why did that question have so many views? Was it on reddit or something?)

I just finished my CS degree 3 years ago (at The Hebrew University of Jerusalem).

We were taught/warned about floating point arithmetic, basically all over the place.

I’ll mention a few courses I took:

1) Computer Architecture – That’s where we studied about caches, TLBs, muxers and floats – Used to be mandatory, just fell of the truck 2 years ago.

2) Experimental Approaches to CS – An elective in which we try to take various CS issues (say: the packing problem, caching strategies…) and actually measure them. Actually a fair amount of the course handles measuring.

3) Numeric Computation – Kind of analogous to Numerical Recipes.

They actually DON’T spend time on that, just mention it every lesson – b/c it’s obvious it’s there.

4) Intro to Machine Learning – again here, don’t teach it, but it’s always mentioned that we obviously have “finite” resolution, and that “64 bit mesh is probably fine enough for this problem”.

5) Computer Graphics – didn’t take it, but it was a big course.

6) CS 101 – I just checked, and it’s mentioned in the first lecture that not every number could be reached, due to finite representation.

Anyhow,

If it’s any indication, I think you can’t finish a CS degree without knowing this. You could get rusty, though…

I would like to offer a possible explanation. Programmers rely on 5 second explanations that are sufficient to solve their immediate problem. The 5 second explanation of floating-point arithmetic is something like, “it’s just keeping track of the number and the decimal point separately, like scientific notation for chemistry”.

In fact, many more-detailed explanations say that floats are numbers represented in scientific notation–with a certain number of significant digits, and a certain number of possible exponents of the base 10. This description leads to the belief that

1.0f + 0.2f + 0.2f

should equal

1.4f

It is not easy to explain why this is not the case.

@Jimmy,

Well the givers of five second explanations need to change their message! If I had to mention one implementation detail, it would not be scientific notation. If you look at the beginner questions, they are asking about simple constants like 0.1 and 0.6, not things like 1.347856223e57. They are probably not even thinking of scientific notation or the limitation on the number of significant digits. And scientific notation is not the root cause anyhow — it’s binary. The first thing we have to tell them is that “decimals” are implemented in binary, and unlike integers, cannot be represented exactly. That explanation will almost certainly prompt more questions.

Thanks for your comment.