Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By JosAH

Thread: Problems with adding double.

  1. #1
    ngoc09 is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Unhappy Problems with adding double.

    I got a really strange problem about adding 2 doubles.

    double a = 2.3 + 3.4;
    System.out.println(a);
    the result will be 5.699999999999.
    Can anybody explain it for me!!!!!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problems with adding double.

    Quote Originally Posted by ngoc09 View Post
    I got a really strange problem about adding 2 doubles.

    double a = 2.3 + 3.4;
    System.out.println(a);
    the result will be 5.699999999999.
    Can anybody explain it for me!!!!!
    Read Goldberg's article and you know all.

    kind regards,

    Jos
    ngoc09 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default Re: Problems with adding double.

    U have to determine the number of digits after the decimal point, this problem is related to the binary representation of doubles and intgers, it usually happens that's why it is better to determine the number of digit after the decimal point u want to represent, this will force the rounding of the result and fix this calculation error
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problems with adding double.

    Quote Originally Posted by baf06 View Post
    U have to determine the number of digits after the decimal point, this problem is related to the binary representation of doubles and intgers, it usually happens that's why it is better to determine the number of digit after the decimal point u want to represent, this will force the rounding of the result and fix this calculation error
    Determining the representation of IEEE754 floaing point numbers doesn't fix any calculation errors. Read the link I supplied in my previous reply.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Money Problems ... double dose not work ..
    By mobosecomin in forum New To Java
    Replies: 7
    Last Post: 02-15-2011, 12:34 AM
  2. Adding an integer to a double?
    By tokoolio in forum New To Java
    Replies: 5
    Last Post: 01-17-2011, 06:57 PM
  3. Replies: 1
    Last Post: 12-17-2010, 09:24 AM
  4. Problems adding an external class
    By jan2321 in forum Eclipse
    Replies: 2
    Last Post: 11-06-2008, 10:34 AM
  5. Adding a double element to a vector
    By peachyco in forum New To Java
    Replies: 5
    Last Post: 11-25-2007, 06:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •