Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree3Likes

Thread: Rouding errors.

  1. #1
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Rouding errors.

    Hey,

    I am a total beginner at Java, and I am wondering why do I get a rounding error in a situation like this:

    Example 1:
    System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1);
    displays 0.5000000000000001 and not 0.5. ?

    Example 2:
    import java.util.Scanner;

    public class Chapter217CountingMonetaryUnits {
    public static void main (String[]args){

    System.out.print("Enter in a number "); // I enter 10,03 and I get a rounding error to 1002 when i * 100 later on.

    //creating Scanner
    Scanner input = new Scanner (System.in);
    double amount = input.nextDouble();

    //Turn the amount into cents. Int because there are no desimals.
    int remainingAmount = (int)(amount * 100); // <----If I change 100 to 1000/10 I get 1003 which is correct




    What can I do in these situations to not get these rounding errors?

    Thanks for the help and feedback:)

  2. #2
    unsensible is offline Member
    Join Date
    Apr 2014
    Posts
    14
    Rep Power
    0

    Default Re: Rouding errors.

    When storing that value in remainingAmount I would suggest keeping it as a double and using Math.Round to round it and only then store it in an int. For whatever reason, java is saving the value for remainingAmount as 1002.9999999 instead of 1003. And if you try to store a number like that in an int it always takes off the decimal value, and thus leaves 1002. But if you use Math.Round it will round it up to the 1003.

    I wish I could tell you why it's adding extra values to the end but I can't really explain that.

  3. #3
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Rouding errors.

    Thanks for the reply.

    I have not used Math.Round before. How do I use it in my code?

    What is the function of Math.Round, will it always round a double to closest full number with no desimals?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Rouding errors.

    Last edited by jim829; 10-01-2014 at 12:51 AM.
    DarrylBurke likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Rouding errors.

    Thank you very much for your reply.
    That article seems to be a very good explination to why it is as it is, but also a fairly complex articel for the level I am on.

    I am not sure how I should change my code to resolve the problem I have above?

    Example 1: I have solved it by adding this to my code:
    import java.text.DecimalFormat;
    DecimalFormat df =new DecimalFormat ("#.##");
    System.out.println(df.format(test));

    Example 2:
    Here I am still unsure to how I should change my code, and why it needs to be done in that way.

    Thank you guys for the feedback and help!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Rouding errors.

    Use cents instead of dollars, so you can use ints (or longs) instead of doubles.

    kind regards,

    Jos
    gimbal2 likes this.
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Rouding errors.

    Quote Originally Posted by JosAH View Post
    Use cents instead of dollars, so you can use ints (or longs) instead of doubles.

    kind regards,

    Jos
    Thank you for the help!
    What is the reason to why it gets messed up if I use doubles? Does it have an easy explenation?

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Rouding errors.

    The fractional part is stored in binary form. Most fractions can only be represented as repeating binary expansions.
    So eventually, as you keep adding these up, you get tiny residual errors in the summation. The addition of .1, five times,
    will add in errors which are not present when you simply start with .5. So the two results will compare differently.
    In fact, since .5 is 1/2 this can be represented exactly in binary as .1

    Edit: I wrote a little program to generate these so you can see the difference.

    .1 = .00011001100110011001100
    .5 = .10000000000000000000000

    .1 is computed a 1/16 + 1/32 + 1/256 + 1/512 and so on

    Regards,
    Jim
    Last edited by jim829; 10-01-2014 at 04:13 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Rouding errors.

    Think of any number between 0 (inclusive) and 1 (exclusive); also think of the fractions 1/2, 1/4, 1/8, 1/16, 1/32 etc (there are 53 numbers in this list). Now, try to create the number 1/10 by picking any numbers from that list at most once and adding them together. You can't and the computer can't do it either.

    kind regards,

    Jos
    Norm likes this.
    Build a wall around Donald Trump; I'll pay for it.

  10. #10
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default Re: Rouding errors.

    Very interesting! I cant say I 100% understand it yet, but I will read the paper you posted and go over your posts till I understand it!
    Very interesting, and great help!

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Rouding errors.

    Another way of thinking of it is, how would you accurately write, as a decimal, the fraction 1/3.

    Well, binary fractions have the same problem with 1/10 (as shown by Jos).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Rouding errors.

    Quote Originally Posted by Tolls View Post
    Another way of thinking of it is, how would you accurately write, as a decimal, the fraction 1/3.
    You can't do that for any fraction 1/p where p is prime (except for p == 2 or p == 5); note that all other decimal expansions of 1/p have repeating fraction digits though.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Rouding errors.

    Well, yes, but 1/3 is something everyone knows from decimal fractions, Mr Clever-Clogs/
    :P
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Rouding errors.

    Quote Originally Posted by JosAH View Post
    You can't do that for any fraction 1/p where p is prime (except for p == 2 or p == 5); note that all other decimal expansions of 1/p have repeating fraction digits though.

    kind regards,

    Jos
    And the general rule is that for any base B, denominators which contain only multiples of the
    prime factors of B will be finite expansions. All others will be repeating. And the repeating part
    will only start after N digits where N is the largest exponent of any prime factor of B in the denominator.
    I believe you can also figure out the period of repetition but I would have to look that up or derive
    it because I can't remember.

    Regards,
    Jim
    Last edited by jim829; 10-01-2014 at 06:21 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Rouding errors.

    Quote Originally Posted by jim829 View Post
    And the repeating part will only start after N digits where N is the largest exponent of any prime factor of B in the denominator.
    Huh? Let B == 10 and let p == 7 (i.e. 1/7) ... yo no comprendo senor ...

    kind regards,

    Jos (<--- stupid look on face)
    Build a wall around Donald Trump; I'll pay for it.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Rouding errors.

    Quote Originally Posted by Tolls View Post
    Well, yes, but 1/3 is something everyone knows from decimal fractions, Mr Clever-Clogs/
    :P
    I don't have clever clogs; I only have shtupid shoes ...

    kindest regards,

    Jos ;-)
    Build a wall around Donald Trump; I'll pay for it.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Rouding errors.

    Quote Originally Posted by JosAH View Post
    Huh? Let B == 10 and let p == 7 (i.e. 1/7) ... yo no comprendo senor ...

    kind regards,

    Jos (<--- stupid look on face)
    I didn't explain it very well so I will try again. B is the base (10 in this case). 7 is
    not a prime factor of the base.

    1. Take any base B where the prime factors are p1, p2, ... pn, each occurring to
    some power. A denominator (D) consisting only of those prime factors will result
    in a finite expansion.

    2. The length (L) of the expansion in (1) is equal to the largest
    exponent of those prime factors occurring in the denominator.

    3. If the denominator contains other primes, not listed in (1) above, then the
    expansion will repeat. The repetition will start after L digits where L is from (2) above.

    4. Let N be the product of those primes that are not factors of the base B. Then the
    length of repetition (K) will be 10^K congruent 1 (mod N). Or for K where
    N evenly divides 10^K - 1.

    Here is an example.

    1/140 = 1/(2^2 * 5 * 7).

    2 and 5 are prime factors of the base, 10. And since 2 is the largest exponent, the
    repeating pattern will start after 2 places to the right of the decimal point. 7 is the only
    prime in the denominator not in the base (10) so the repetition will be K digits long when
    7 evenly divides 10^K - 1. That happens when K == 6;


    My source for this information is Number Theory and Its History, Oystein Ore, published
    by Dover Publications, 1948. ISBN 0-486-65620-9. It is still in print.

    The book is very well written and only requires a fairly solid understanding of algebra (what
    we refer to as algebra II in the states). Some of the examples are of course dated (like the
    largest Mersenne prime at the time).

    Regards,
    Jim
    Last edited by jim829; 10-02-2014 at 04:27 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Rouding errors.

    Ah, it was point 3) I didn't understand in your original explanation; it's the number of digits to the left of the repeating digits. Thanks for the explanation (it was just a language thingie ;-)

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Rouding errors.

    How do you two manage to fit through doorways with brains that size?

    ;)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Rouding errors.

    Quote Originally Posted by Tolls View Post
    How do you two manage to fit through doorways with brains that size?

    ;)
    I don't; I'm too stupid to even find the dooway.

    kindest regards,

    Jos *boink*
    Build a wall around Donald Trump; I'll pay for it.

Page 1 of 2 12 LastLast

Similar Threads

  1. Rouding
    By nepperso in forum NetBeans
    Replies: 1
    Last Post: 09-14-2013, 03:09 PM
  2. need help to fix my errors
    By ali1 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 06:19 PM
  3. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  4. Errors?
    By Jamison5213 in forum New To Java
    Replies: 4
    Last Post: 12-30-2009, 12:14 AM
  5. Replies: 3
    Last Post: 03-08-2009, 01:51 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
  •