Problem with simple substract

D

Damien

Hi,

Could you tell me why this :

public class A {
public static void main (String[] args) {
double d1 = 1.4;
double d2 = 1.1;
double res = d1-d2;
Double RES = new Double(res);

System.out.println("double >> " + res);
System.out.println("DOUBLE >> " + RES);
}
}

Display this result :
double >> 0.2999999999999998
DOUBLE >> 0.2999999999999998

instead of 0.3

thank
 
C

Christophe Vanfleteren

Damien said:
Hi,

Could you tell me why this :

public class A {
public static void main (String[] args) {
double d1 = 1.4;
double d2 = 1.1;
double res = d1-d2;
Double RES = new Double(res);

System.out.println("double >> " + res);
System.out.println("DOUBLE >> " + RES);
}
}

Display this result :
double >> 0.2999999999999998
DOUBLE >> 0.2999999999999998

instead of 0.3

thank

http://groups.google.com/groups?th=163d86a93327a06
 
A

Adam

Hi,
Could you tell me why this :

public class A {
public static void main (String[] args) {
double d1 = 1.4;
double d2 = 1.1;
double res = d1-d2;
Double RES = new Double(res);

System.out.println("double >> " + res);
System.out.println("DOUBLE >> " + RES);
}
}

Display this result :
double >> 0.2999999999999998
DOUBLE >> 0.2999999999999998

instead of 0.3

I suspect a major bug in your CPU...
or maybe in java core...
or maybe it's just the computers are stupid
and don't know how to subtract...




















:))))))))))
Seriously:
There's no way of having 0.3 (3/10) in binary system
(just like you can't have 1/3 in decimal system: 0.333333....)
Your result is an approximation, like most (all?) of floating-point
operations.

Adam
 
J

Jon Skeet

Damien said:
Could you tell me why this :

public class A {
public static void main (String[] args) {
double d1 = 1.4;
double d2 = 1.1;
double res = d1-d2;
Double RES = new Double(res);

System.out.println("double >> " + res);
System.out.println("DOUBLE >> " + RES);
}
}

Display this result :
double >> 0.2999999999999998
DOUBLE >> 0.2999999999999998

instead of 0.3

See http://www.pobox.com/~skeet/csharp/floatingpoint.html - it's a
..NET/C#-based article, but it applies equally well to Java.
 
L

Larry Coon

Damien said:
Could you tell me why this : (snip)
Display this result :
double >> 0.2999999999999998
DOUBLE >> 0.2999999999999998

instead of 0.3

Remember that computers represent numbers in binary, not base 10.
Any whole number can be accurately represented in binary, but decimal
values often cannot. A number that can be cleanly represented in
base 10, such as 0.1, can be troublesome in binary. As an exercise,
try finding a binary representation for 0.1, i.e., values for a, b,
c, ..., where a, b, c, ... are either 0 or 1, such that:

0.1 = a*2^-1 + b*2^-2 + c*2^-3 + ...

(I'm using ^ for exponentiation)

Floating point formats, such as IEEE 754, are therefore considered
approximations only. For some values they'll be dead-on, but for
other values you'll end up with results like the one you describe
above. A common mistake people make is to assume that if a number
can be accurately represnted in base 10, it can be accurately
represented in base 2 as well.

(PS: Base 10 suffers from this same problem. Try representing
1/3 or Pi with 100% accuracy in base 10 -- you can't.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top