Reputation: 34760
I have the following statement:
float diff = tempVal - m_constraint.getMinVal();
tempVal
is declared as a float and the getMinVal()
returns a float value.
I have the following print out:
diff=0.099999905, tempVal=5.1, m_constraint.getMinVal()=5.0
I expect the diff is 0.1 but not the above number. how to do that?
Upvotes: 5
Views: 9837
Reputation: 15641
Floats use the IEEE754
to represent numbers, and that system has some rounding errors.
What Every Computer Scientist Should Know About Floating-Point Arithmetic
Bottom-line if you are doing arithmetic and it needs to be exact don't use float
or double
but us BigDecimal
Upvotes: 4
Reputation: 21795
Because of the way they store values internally, floats and doubles can only store completely accurately numbers which can be decomposed into a sum of powers of 2 (and then, within certain constraints relating to their absolute and relative magnitude).
So as soon as you attempt to store, or perform a calculating involving, a number which cannot be stored exactly, you are going to get an error in the final digit.
Usually this isn't a problem provided you use floats and doubles with some precaution:
use a size of floating point primitive which has "spare" digits of precision beyond what you need;
for many applications, this probably means don't use float at all (use double instead): it has very poor precision and, with the exception of division, has no performance benefit on many processors;
when printing FP numbers, only actually print and consider the number of digits of precision that you need, and certainly don't include the final digit (use String.format to help you);
if you need arbitrary number of digits of precision, use BigDecimal instead.
Upvotes: 1
Reputation: 27233
Java encodes real numbers using binary floating point representations defined in IEEE 754. Like all finite representations it cannot accurately represent all real numbers because there is far more real numbers than potential representations. Numbers which cannot be represented exactly (like 0.1 in your case) are rounded to the nearest representable number.
Upvotes: 0
Reputation: 25705
You cannot get exact
results with float
ing point numbers. You might need to use a FixedPoint library for that. See : http://sourceforge.net/projects/jmfp/
Upvotes: 0