Double addition problem

I am running the following code and it is producing the output shown below the code.

CODE

public class Test {

public static void main (String[]args) {

Double d = new Double("5.0");

Double t = new Double("5.53");

System.out.println(d+" "+t);

System.out.println(d.doubleValue()+t.doubleValue());

}

/** Creates a new instance of Test */

public Test() {

}

}

OUTPUT

5.0 5.53

10.530000000000001

Surely the addition of the two doubles should equal 10.53

Can anyone help me with this

[591 byte] By [Patrick-McDonogh] at [2007-9-30 14:50:03]
# 1

Hi,

i cannot explain the typical behaviour, However if you convert the result into a float it gives the correct answer (10.53).

float dd2 = (float) (5.53 + 5.0);

System.out.println("dd2 = "+dd2);

I do not know if this is the correct approach, but your question is really interesting interms of internal working of doubles.

bye

mm

MAHIM_MISHRA at 2007-7-5 20:59:20 > top of Java-index,Administration Tools,Sun Connection...
# 2

Yea I thought about this too and tried some small c programs as well.

#include <iostream>

#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])

{

printf("%f", (5.0f+5.53f));

system("PAUSE");

return 0;

}

and got

10.530001

Then for double precision

#include <iostream>

#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])

{

printf("%f", (5.0+5.53));

system("PAUSE");

return 0;

}

got 10.53

With %g formatter both spit out 10.53

This has to do with two's-complement floating math computation, http://support.microsoft.com/default.aspx?scid=kb;EN-US;q42980

The way to improve your computation accuracy is get a 64 bit processor:-)

Martin3 at 2007-7-5 20:59:20 > top of Java-index,Administration Tools,Sun Connection...
# 3

Doubles and floats in Java are stored as floating-point binary (base 2) numbers. Some base 10 decimal numbers cannot be represented exactly by binary decimals, as happened to you. This is normal and expected. See this search listing for further discussions and details.

http://onesearch.sun.com/search/developers/index.jsp?and=%2Bbinary+%2Bfloating+%2Bpoint+%2Bprecision&nh=100&phr=&qt=&not=&field=&since=&col=devforums&rf=0&Search.x=25&Search.y=8

If you want exactness, use the BigDecimal class, as it handles floating-point decimal numbers, or format the displayed number using the NumberFormat and Decimal Format classes.

ChuckBing at 2007-7-5 20:59:20 > top of Java-index,Administration Tools,Sun Connection...
# 4

> The way to improve your computation accuracy is get a 64 bit processor:-)

umm...As long as the same standard is used to represent a number you will get the same result, regardless of the computer or whether the processor is 8 or 16 or 32 or 64 or .... bits.

The creators of Java determined what standard would be used. Java doubles are IEEE 754 double precision binaries, 64 bits long. To make them more precise it would be necessary to change Java to use another representation, for instance IEEE 754 quad precision, which uses 128 bits to represent the number.

ChuckBing at 2007-7-5 20:59:20 > top of Java-index,Administration Tools,Sun Connection...
# 5

Hi,

Here is the code.

public class TestCondition

{

public static void main(String args[])

{

Double d=new Double("5.0");

Double d1=new Double("5.53");

System.out.println(" Res - >"+(float)(d.doubleValue()+d1.doubleValue()));

}

}

Result 10.53 exacty. No fun output

VisionSuresh at 2007-7-5 20:59:21 > top of Java-index,Administration Tools,Sun Connection...
# 6

Yea but if you take out the float cast you'll get the round off error again.

If you are working with numbers for example monetary calculations you can employ http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html. I have a handy extension in the case where you need to stay within a number of significant digits at:

http://cvs.sourceforge.net/viewcvs.py/mathml-x/mathml-x/src/com/neuralworks/text/DynamicDecimalFormat.java?rev=1.1&view=markup

Martin3 at 2007-7-5 20:59:21 > top of Java-index,Administration Tools,Sun Connection...