Android - exact mathematical calculation

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I have got a Problem, I am developing an Application which should be able to do some mathematic calculations. These calculations have to be exact (or rather not obviously wrong)

But this simple Code

double a = 3.048d; double b = 1000d;  double c = a / b; 

gives me a wrong result c is not 0.003048 as expected instead it is 0.0030480000000000004 which is obviously wrong.

double d = 3.048 / 1000;  

this second code-snipet gives the correct result.

I am aware that all floatingpoint arithmetic is not exact when calculating with computers but I don't know how to solve this problem.

thanks in advance!
Ludwig

Developing for:
- Android 2.2
Testdevice:
- HTC Desire

回答1:

What you need to use for exact percision is the BigDecimal object:

BigDecimal a = new BigDecimal("3.048"); BigDecimal b = new BigDecimal(1000);  BigDecimal c = a.divide(b);  System.out.println(c); //0.003048 


回答2:

This is a consequence of the IEEE 754 floating point representation, not an error. To deal with it, round your result to an appropriate precision.



回答3:

Use a BigDecimal for precise floating point calculations. Setting the scale allows you to specify precisely how far out you want to go for output.

import java.math.BigDecimal;  class Test{          public static void main(String[] args){                 BigDecimal a = new BigDecimal("3.048");                 BigDecimal b = new BigDecimal(1000);                 BigDecimal c = a.divide(b).setScale(6);                 System.out.println(c); //0.003048         } } 


回答4:

Use BigDecimal for such precise allocations.

Btw the result for d is obviously right, because double has a machine encoding which cannot store the result, which you perceive as correct.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!