G*********a 发帖数: 1080 | 1 I met a problem of losing precision and please help! I am calculating an
accumulative value shich is quite small, it's the multiplication result of
some numbers look like these:
0.6333333333333334 0.002551020408163265 0.35 0.004251700680272109
0.1496794871794872 0.04421768707482993
0.34761904761904766 0.005952380952380952 0.31 0.008503401360544218
0.17910052910052912 0.05357142857142857
I tried to set the result value as either a Double or a BigDecimal, but | M*****r 发帖数: 1536 | 2 What JDK version r u using?
Also, there are some error/typo in the code you posted.
This works for me in JDK 1.5.0_07.
public class numbertest
{
public static void main(String[] args){
Double a = 0.0017006802721088435;
Double b = 0.0;
Double c = 0.55;
Double d = 0.0;
Double resultInDouble = 1/2*(a-b)*(c+d);
BigDecimal aa = new BigDecimal("0.0017006802721088435");
BigDecimal bb = new BigDecimal("0.0");
BigDecimal cc = new BigDecimal("0.55");
BigDecimal dd = new BigDecimal("0.0");
BigDecimal resul
【在 G*********a 的大作中提到】 : I met a problem of losing precision and please help! I am calculating an : accumulative value shich is quite small, it's the multiplication result of : some numbers look like these: : 0.6333333333333334 0.002551020408163265 0.35 0.004251700680272109 : 0.1496794871794872 0.04421768707482993 : 0.34761904761904766 0.005952380952380952 0.31 0.008503401360544218 : 0.17910052910052912 0.05357142857142857 : I tried to set the result value as either a Double or a BigDecimal, but
| G*********a 发帖数: 1080 | 3 1.5. sorry, i did type something wrong above.
thank u very much for ur testing, i also found the problem of my previous
code was mainly due to 1/2 in java in an interger type 0, i should have used
0.5 instead.
【在 M*****r 的大作中提到】 : What JDK version r u using? : Also, there are some error/typo in the code you posted. : This works for me in JDK 1.5.0_07. : public class numbertest : { : public static void main(String[] args){ : Double a = 0.0017006802721088435; : Double b = 0.0; : Double c = 0.55; : Double d = 0.0;
|
|