Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Problem with simple substract

Reply
Thread Tools

Problem with simple substract

 
 
Damien
Guest
Posts: n/a
 
      12-10-2003
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
 
Reply With Quote
 
 
 
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      12-10-2003
Damien wrote:

> 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

--
Kind regards,
Christophe Vanfleteren
 
Reply With Quote
 
 
 
 
Adam
Guest
Posts: n/a
 
      12-10-2003
> 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


 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      12-10-2003
(Damien) writes:
> Could you tell me why this :


Read an archive of the group. And post such beginner's questions to
comp.lang.java.help next time.

/Thomas
 
Reply With Quote
 
Jon Skeet
Guest
Posts: n/a
 
      12-10-2003
Damien <> wrote:
> 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.

--
Jon Skeet - <>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Larry Coon
Guest
Posts: n/a
 
      12-10-2003
Damien wrote:

> 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.)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it conformant to substract two pointer-to-void ? Francois Grieu C Programming 10 03-14-2008 01:26 AM
Bug: Time#-(1e-6) doesn't substract one microsecond. Dmitry Maksyoma Ruby 0 05-17-2006 05:53 AM
DateTime Substract CBN Media ASP .Net 3 03-02-2004 01:33 AM
Substract numeric from a string. Hon Seng Phuah Perl 3 02-26-2004 11:46 AM
stack values in xsl or substract element from tree Jean-Christophe Michel XML 4 01-05-2004 08:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57