This content has been marked as final.
Show 5 replies

1. Re: Inaccuracy in float calculations?
807605 Sep 9, 2007 7:58 AM (in response to 807605)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 noninteger type.
Good luck! 
2. Re: Inaccuracy in float calculations?
796254 Sep 9, 2007 8:25 AM (in response to 807605)Read this:
http://docs.sun.com/source/8063568/ncg_goldberg.html
% 
3. Re: Inaccuracy in float calculations?
807605 Sep 9, 2007 8:49 AM (in response to 807605)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?
DarrylBurke Sep 9, 2007 9:58 AM (in response to 807605)I have a float x that every 20ms gets 0.2f
A quick fix for your specific situation, provided the computational error in the final value is acceptable:
added to it up to 4.0f max.
Set the upper limit at 4.1f
dbfloat f = 0f; while (f < 4.1f) { System.out.println(Float.toString(f)); // prints up to 4.0000005 f += 0.2f; }
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?
796254 Sep 9, 2007 10:08 AM (in response to DarrylBurke)
I wouldn't consider this an acceptable "fix" for anything.I have a float x that every 20ms gets 0.2f
A quick fix for your specific situation, provided the
added to it up to 4.0f max.
computational error in the final value is
acceptable:
Do the computation unvarnished. Worry about the rounding when you display or persist.
Serious calculations require some study of numerical methods.
%