Results 1 to 20 of 22
Thread: Rouding errors.
 09302014, 09:28 PM #1Member
 Join Date
 Sep 2014
 Posts
 47
 Rep Power
 0
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:)
 09302014, 09:45 PM #2Member
 Join Date
 Apr 2014
 Posts
 14
 Rep Power
 0
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.
 09302014, 11:07 PM #3Member
 Join Date
 Sep 2014
 Posts
 47
 Rep Power
 0
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?
 10012014, 12:47 AM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
Re: Rouding errors.
Read about it here > What every computer scientist should know about floating point arithmetic
Regards,
JimLast edited by jim829; 10012014 at 12:51 AM.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 10012014, 11:52 AM #5Member
 Join Date
 Sep 2014
 Posts
 47
 Rep Power
 0
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!
 10012014, 01:20 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
Re: Rouding errors.
Use cents instead of dollars, so you can use ints (or longs) instead of doubles.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 10012014, 03:32 PM #7Member
 Join Date
 Sep 2014
 Posts
 47
 Rep Power
 0
 10012014, 04:00 PM #8Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
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,
JimLast edited by jim829; 10012014 at 04:13 PM.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 10012014, 04:06 PM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 10012014, 04:22 PM #10Member
 Join Date
 Sep 2014
 Posts
 47
 Rep Power
 0
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!
 10012014, 04:34 PM #11Moderator
 Join Date
 Apr 2009
 Posts
 13,541
 Rep Power
 27
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 **
 10012014, 05:13 PM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
 10012014, 05:26 PM #13Moderator
 Join Date
 Apr 2009
 Posts
 13,541
 Rep Power
 27
Re: Rouding errors.
Well, yes, but 1/3 is something everyone knows from decimal fractions, Mr CleverClogs/
:PPlease do not ask for code as refusal often offends.
** This space for rent **
 10012014, 06:16 PM #14Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
Re: Rouding errors.
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,
JimLast edited by jim829; 10012014 at 06:21 PM.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 10012014, 07:34 PM #15
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
 10012014, 07:55 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
 10012014, 08:20 PM #17Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 14
Re: Rouding errors.
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 0486656209. 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,
JimLast edited by jim829; 10022014 at 04:27 AM.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 10022014, 07:16 AM #18
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 10022014, 09:24 AM #19Moderator
 Join Date
 Apr 2009
 Posts
 13,541
 Rep Power
 27
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 **
 10022014, 10:02 AM #20
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
Similar Threads

Rouding
By nepperso in forum NetBeansReplies: 1Last Post: 09142013, 03:09 PM 
need help to fix my errors
By ali1 in forum New To JavaReplies: 8Last Post: 11172011, 06:19 PM 
First Java ProgramCompile Errors (errors are posted)simple GUI
By cc11rocks in forum AWT / SwingReplies: 4Last Post: 01042011, 12:36 AM 
Errors?
By Jamison5213 in forum New To JavaReplies: 4Last Post: 12302009, 12:14 AM 
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03082009, 01:51 AM
Bookmarks