由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - help: Double or BigDecimal
相关主题
Test your PC speed请教:怎样函数里改变一个Double变量的值?
Java on AIXVECTOR一问
java里的时间计算java 里怎么限定小数点的位置
what is the problem?如何定义近似值并进行相应的计算
java beginner question高手麻烦帮帮忙。。新手学编程
multiple classes in one file:revisted火车旅行家在中文WINDOWS下无法运行的大问题已被解决
有关thread in JavaRe: Question on mouse click on image in Jdk 1.1
IBM JRE6.0How do a Servlet sent a Applet?
相关话题的讨论汇总
话题: bigdecimal话题: double话题: result话题: value话题: shich
进入Java版参与讨论
1 (共1页)
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;

1 (共1页)
进入Java版参与讨论
相关主题
How do a Servlet sent a Applet?java beginner question
折腾了一天,实在是绝望了,请教请教multiple classes in one file:revisted
What version of the Java Development Kit (JDKTM)有关thread in Java
Java向Linux迈出决定性一步IBM JRE6.0
Test your PC speed请教:怎样函数里改变一个Double变量的值?
Java on AIXVECTOR一问
java里的时间计算java 里怎么限定小数点的位置
what is the problem?如何定义近似值并进行相应的计算
相关话题的讨论汇总
话题: bigdecimal话题: double话题: result话题: value话题: shich