How to avoid rounding errors in Android app devlopment?
I'm doing what seems like simple math in an android app, however I keep getting massive rounding errors. For example.
Float.toString(0.4*0.4*12.5);
I would expect to get 2 as the result of the above equation. However I get 2.0开发者_Go百科000002
0.4 can't be represented exactly in a 32-bit IEEE-754 float. See:
- http://babbage.cs.qc.edu/IEEE-754/Decimal.html
- http://floating-point-gui.de/
- http://docs.sun.com/source/806-3568/ncg_goldberg.html
If you absolutely need arbitrary precision use BigDecimal. Arbitrary precision math is slower, though. Or use 64-bit math (doubles) to improve the precision.
Floating point is an approximate number system, in your case 0.4 cannot be represented exactly using a floating point number, so it is represented as close as possible, so you don't get the expected result.
Is there a Round function for Android that you can use?
So I ended up making two functions, one to round Doubles and one for Floats. Seems like a PITA, but other then being limited to a preset level of precision (which I'll look at making a app setting) it does work.
public static float fRound(float Rval, int Rpl) {
float p = (float)Math.pow(10,Rpl);
Rval = Rval * p;
float tmp = Math.round(Rval);
return (float)tmp/p;
}
public static Double dRound(double d, int decimalPlace){
BigDecimal bd = new BigDecimal(Double.toString(d));
bd = bd.setScale(decimalPlace,BigDecimal.ROUND_HALF_UP);
return bd.doubleValue();
}
精彩评论