5 Replies Latest reply: Sep 9, 2007 8:25 AM by 796254

# Inaccuracy in float calculations?

Hi

I am making a small game and every 20ms I am doing some plusses and minuses on float values. However, I have a problem with my calculations not adding up. For instance I have a float x that every 20ms gets 0.2f added to it up to 4.0f max. However, it never reaches this value since its more like 3.80000000000007 instead. How can I fix this?
• ###### 1. Re: Inaccuracy in float calculations?
a couple of points

1) By the very nature of its representation on a computer, floating point will never by completely accurate. You will have to assume that and work around that.

2) I don't know of hardly any situations where you would want to use float type rather than the more accurate double type.

3) Consider using other types. If dealing with time, rather than using floating point representation of seconds, how about integer or long representation of milliseconds. Consider using the BigDecimal type if you really need accurate non-integer type.

Good luck!
• ###### 2. Re: Inaccuracy in float calculations?
Read this:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

%
• ###### 3. Re: Inaccuracy in float calculations?
Perhaps I should just use integers and a multiple of 10 during calculations then...and then just divide by 10 before using. That would give me the same effect and more accurate...except a couple of more steps ofcourse...
• ###### 4. Re: Inaccuracy in float calculations?
I have a float x that every 20ms gets 0.2f
added to it up to 4.0f max.
A quick fix for your specific situation, provided the computational error in the final value is acceptable:
Set the upper limit at 4.1f
``````        float f = 0f;
while (f < 4.1f) {
System.out.println(Float.toString(f)); // prints up to 4.0000005
f += 0.2f;
}``````
db

edit Great link, duffymo, thanks.
Must save the page if I can get it to open fully.

Message was edited by:
Darryl.Burke
• ###### 5. Re: Inaccuracy in float calculations?
I have a float x that every 20ms gets 0.2f
added to it up to 4.0f max.
A quick fix for your specific situation, provided the
computational error in the final value is
acceptable:
I wouldn't consider this an acceptable "fix" for anything.

Do the computation unvarnished. Worry about the rounding when you display or persist.

Serious calculations require some study of numerical methods.

%