While writing codes we all do various mistakes that lead us to errors like overflow errors and syntax errors. Rounding-off error is one of the common errors that may result in a wrong solution for a given mathematical problem. Generally, this error occurs when we work with floating point numbers.
In this article, we will explore the causes of this problem and try to find a way to get rid of this error.
They are also called real numbers and are used when calculations require fractional values. It represents a number with a decimal point. For example, to express the result of 1/5, which is 0.2, the results of sine and cosine also require a decimal point.
In Java, there are two types of floating point numbers -
Floating point type - It can store single precision values up to 32 bits. We declare it using the 'float' keyword.
Double − Its size is larger than float and can store double precision values up to 64 bits. It is declared using 'double' keyword.
Let's discuss an example where we might encounter a rounding error and then we will understand the cause and provide a solution to handle it correctly.
public class Main{ public static void main(String[] args) { double data1 = 0.30; double data2 = 0.20 + 0.10; System.out.println("Value after addition: " + data2); System.out.println(data1 == data2); double data3 = 0.50; double data4 = 0.10 + 0.10 + 0.10 + 0.10 + 0.10; System.out.println("Value after addition: " + data4); System.out.println(data3 == data4); } }
Value after addition: 0.30000000000000004 false Value after addition: 0.5 true
In the above code, when we add it for the first time, the expected value of 'data2' is 0.30, but the output result is wrong. However, when we perform another similar addition operation, we get the exact result.
Let’s take another example that shows an error.
The Chinese translation ofpublic class Main { public static void main(String[] args) { double data1 = 4.30452; double data2 = 0.503542; double res = data1 + data2; System.out.println("Value after addition: " + res); } }
Value after addition: 4.8080620000000005
The expected output is 4.808062, but we get the wrong output here.
The 'IEEE 754' is a standard that is followed by Java while implementing floating point numbers. According to its 'Rounding' rule, when than value of mantissa is greater than 23 bits then it adds 1 to the 23rd bit to round the value. Here, the mantissa is the binary representation of fractional digits.
This is why we get different values.
In order to avoid this rounding error, we have the following methods -
BigDecimal − It is a class that can be used in the place of floating point numbers like float and double. We can perform all the normal operations that float and double datatypes provide. For example , arithmetic operations, comparison and rounding. It can handle these operations with accurate precision.
The following example demonstrates how we can use it to produce an accurate result.
import java.math.*; public class Main{ public static void main(String[] args) { BigDecimal data1 = new BigDecimal("0.20"); BigDecimal data2 = new BigDecimal("0.10"); BigDecimal res = data1.add(data2); // performing addition System.out.println("Value after addition: " + res); } }
Value after addition: 0.30
In example 1, we got an error while adding two values. In the above example, we used the same values but this time we got the correct result.
Math.round() − It is a static method of class ‘Math’. It takes a floating point value as argument and returns the nearest integer value. We call it using its class name.
The below example illustrates its use in the program.
public class Main { public static void main(String[] args) { double data1 = 4.30452; double data2 = 0.503542; double res = data1 + data2; System.out.println("Value after addition: " + res); System.out.println("Value after rounding off the addition: " + Math.round(res)); } }
Value after addition: 4.8080620000000005 Value after rounding off the addition: 5
Rounding off error is not a compile time or runtime error, the Java compiler cannot detect it. This is the reason why the programmer needs to pay extra attention to these errors otherwise we will get the wrong result. In this article, we have discussed the reason and also suggested some ways to overcome this rounding-off error.
The above is the detailed content of Rounding error in Java. For more information, please follow other related articles on the PHP Chinese website!