Rounding off errors in Java
Compacting many infinite real numbers into a finite number of bits requires an approximate representation. Most programs store the result of integer computations 32 or 64 bits max. Given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is a characteristic feature of floating-point computation.
Therefore, while handling calculations in floating point numbers, (especially if calculations are in term of money), we need to take care of round off errors in a programming language.
Let’s see an example:
public class Main { public static void main(String[] args) { double a = 0.7 ; double b = 0.9 ; double x = a + 0.1 ; double y = b - 0.1 ; System.out.println( "x = " + x); System.out.println( "y = " + y ); System.out.println(x == y); } } |
Output:
x = 0.7999999999999999 y = 0.8 false
Here, the answer is not what we expected reason being the rounding off done by java compiler.
Reason behind round off error
float and double data types implement IEEE floating point 754 specification. This means that numbers are represented in a form like:
SIGN FRACTION * 2 ^ EXP
0.15625 = (0.00101)_{2}, which in floating-point format is represented as: 1.01 * 2^-3
Not all fractions can be represented exactly as a fraction of a power of two. As a simple example, 0.1 = (0.000110011001100110011001100110011001100110011001100110011001… )_{2} and thus cannot be stored inside a floating-point variable.
Another Example:
public class Main { public static void main(String[] args) { double a = 0.7 ; double b = 0.9 ; double x = a + 0.1 ; double y = b - 0.1 ; System.out.println( "x = " + x); System.out.println( "y = " + y ); System.out.println(x == y); } } |
Output:
x = 0.7999999999999999 y = 0.8 false
Another example:
public class Main { public static void main(String args[]) { double a = 1.0 ; double b = 0.10 ; double x = 9 * b; a = a - (x); // Value of a is expected as 0.1 System.out.println( "a = " + a); } } |
Output:
a = 0.09999999999999998
How to rectify round off errors?
- Round the result: The Round() function can be used to minimize any effects of floating point arithmetic storage inaccuracy. The user can round numbers to the number of decimal places that is required by the calculation. For example, while working with currency, you would likely round to 2 decimal places.
- Algorithms and Functions: Use numerically stable algorithms or design your own functions to handle such cases. You can truncate/round digits of which you are not sure they are correct (you can calculate numeric precision of operations too)
- BigDecimal Class: You may use the java.math.BigDecimal class, which is designed to give us accuracy especially in case of big fractional numbers.
The following program shows how the error can be removed:
public
class
Main {
public
static
void
main(String args[])
{
double
a =
1.0
;
double
b =
0.10
;
double
x =
9
* b;
a = a - (x);
/* We use Math.round() function to round the answer to
closest long, then we multiply and divide by 1.0 to
to set the decimal places to 1 place (this can be done
according to the requirements.*/
System.out.println(
"a = "
+ Math.round(a*
1.0
)/
1.0
);
}
}
chevron_rightfilter_noneOutput:
0.1
Now we get the expected output, without the error.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- rint() in Java (Rounding to int)
- Errors V/s Exceptions In Java
- Thread Interference and Memory Consistency Errors in Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java lang.Long.reverse() method in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.concurrent.Phaser class in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java.util.concurrent.RecursiveTask class in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.