Problem with simple substract

Discussion in 'Java' started by Damien, Dec 10, 2003.

  1. Damien

    Damien Guest

    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
    Damien, Dec 10, 2003
    #1
    1. Advertising

  2. 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
    Christophe Vanfleteren, Dec 10, 2003
    #2
    1. Advertising

  3. Damien

    Adam Guest

    > 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
    Adam, Dec 10, 2003
    #3
  4. (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
    Thomas Weidenfeller, Dec 10, 2003
    #4
  5. Damien

    Jon Skeet Guest

    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
    Jon Skeet, Dec 10, 2003
    #5
  6. Damien

    Larry Coon Guest

    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.)
    Larry Coon, Dec 10, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hon Seng Phuah

    Substract numeric from a string.

    Hon Seng Phuah, Feb 25, 2004, in forum: Perl
    Replies:
    3
    Views:
    526
    Joe Smith
    Feb 26, 2004
  2. CBN Media

    DateTime Substract

    CBN Media, Mar 2, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    13,256
    CBN Media
    Mar 2, 2004
  3. Jean-Christophe Michel

    stack values in xsl or substract element from tree

    Jean-Christophe Michel, Dec 24, 2003, in forum: XML
    Replies:
    4
    Views:
    427
    Dimitre Novatchev
    Jan 5, 2004
  4. Francois Grieu

    Is it conformant to substract two pointer-to-void ?

    Francois Grieu, Mar 12, 2008, in forum: C Programming
    Replies:
    10
    Views:
    587
    Gordon Burditt
    Mar 14, 2008
  5. Dmitry Maksyoma
    Replies:
    0
    Views:
    89
    Dmitry Maksyoma
    May 17, 2006
Loading...

Share This Page