Results 1 to 3 of 3
  1. #1
    jackripley is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default "double" data type problem

    Hi there,
    can anyone tell me what happen to this code? how come the print out is 109.99999999999943 instead of 110?

    public static void main(String args[]){
    double num=100;
    for(int i=0; i<100; i++){
    num+=0.1;
    }
    System.out.println(num);
    }

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

    Default Re: "double" data type problem

    Read this article, your problem boils down to the fact that 0.1 cannot be exactly represented in the IEEE/754 format.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jackripley is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. problem with enctype="multipart/form-data" in the JSP page
    By varunphilip in forum Advanced Java
    Replies: 0
    Last Post: 11-19-2011, 07:11 AM
  2. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  3. Selecting the greatest "double" in a string array
    By gangsterooseven in forum New To Java
    Replies: 6
    Last Post: 11-07-2009, 11:37 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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