View Full Version : Float rounding errors in simple program
Halide
09-18-2003, 01:59 PM
First post in a while --hey!
OK, I'm using a float variable, and a resultant value is 10.825. Using setprecision(2) with std::cout yields 10.82 instead of 10.83. I realize that a small precision error would actually cause the value of 10.825 to be 10.8249999... which is revealed using setprecision(10). So, what gives? How should I store a correctly rounded dollar value?
Flangazor
09-19-2003, 08:06 AM
Since C++ truncates basic values instead of rounding, you can round to whichever precision you like by adding 5*10^-x where x is the precision.
0.2 rounded (0.2 + 0.5) becomes 0.
0.5 rounded (0.5 + 0.5) becomes 1.
0.02 becomes 0.0.
0.06 becomes 0.11 -> 0.1.
etc.
gl hf
Halide
09-19-2003, 10:37 AM
But, if you add 0.05 to a 2 precision float, it could possibly cause rounding errors as well.
Suppose you have 10.8249999 -- which is supposed to be 10.825. By adding 0.05, you increase the value to 10.8749999, which still rounds to 10.87 instead of 10.88...
Flangazor
09-19-2003, 01:24 PM
Not at all.
10.8249999 + 0.0005 = 10.825499 which truncates to 10.825
10.874999 + 0.0005 = 10.875499 which truncates to 10.875
This is correct behaviour because 10.874999 SHOULD round to 10.87 and should NOT round to 10.88.
gl hf
Halide
09-19-2003, 03:42 PM
I think I'm not being clear...
First of all, I did some calculations, and I get the value of 10.825...in reality it's not -- it's offset to 10.824999...you see what I'm saying?
It should NOT round down because the value SHOULD be 10.825
btw, I'm using Dev-C++ on Win XP, if this matters...
jamessan
09-19-2003, 04:01 PM
It's not going to matter which compiler you use. This is a limitation of trying to express floating point numbers in binary.
Interesting link regarding floating point approximations (http://www.unna.org/unna/apple/documentation/developer/QAs-2.x/html/floatpnt.htm)
Halide
09-19-2003, 08:50 PM
Ah...nice link.
so, I could just store it as an integer, and divide by 100 :) That is prettier than adding 0.001 to the float! :D thx
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.