Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Silly error

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Silly error

    System.out.println(2.6%1.1)// gives 0.39999999999
    System.out.printf("%f",2.6%1.1)//gives 0.40000000

    Anyone know why ?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Silly error

    Because floating point numbers have limited precision. So, when you calculate the modulus, the slight error due to the hardware limitations (known as the "ulp" or "unit in the last place") makes the number slightly off from exactly 0.4.

    Printf is likely rounding off items within a few ulps so it prints out 0.4, but println doesn't so it prints out the number that's slightly off. This is also why using the == operator with floating points is very dangerous (and rather silly).

  3. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    American (August 23rd, 2010), Brt93yoda (August 23rd, 2010)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Silly error

    In the first case, the parameter numbers are treated as doubles (the default for primitive floating point numbers in java). The second instance it is being interpreted as a float. As an example:
    System.out.println(2.6%1.1)// gives 0.39999999999
    System.out.println((float)(2.6%1.1))// gives 0.4
    System.out.printf("%f",2.6%1.1)//gives 0.40000000
    Last edited by copeg; August 22nd, 2010 at 08:35 PM.

  5. The Following 2 Users Say Thank You to copeg For This Useful Post:

    American (August 23rd, 2010), Brt93yoda (August 23rd, 2010)

  6. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Silly error

    oh, well it's rounding off more than a few ulps

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    American (August 23rd, 2010)

  8. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Silly error

    Thanks to you all. Interesting....
    Last edited by American; August 23rd, 2010 at 03:55 PM.

  9. #6
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Silly error

    I actually didn't know this either. Thank you Copeg and HelloWorld922!

Similar Threads

  1. Im getting 1 Silly error can someone help please
    By JavaNoob82 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 24th, 2010, 06:55 PM