Redian新闻
>
help: Double or BigDecimal
avatar
help: Double or BigDecimal# Java - 爪哇娇娃
G*a
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
avatar
M*r
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

avatar
G*a
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;

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。