# Rounding off errors in Java

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 reason for this problem and try to find a way to get rid of this kind of error.

## Rounding off Errors

### Floating Point Types

They are also known as real numbers and are used when the calculation requires fractional values. It represents numbers that have decimal points. For example, to represent result of 1/5 i.e. 0.2, results of sine and cosine also require decimal points.

In Java, there are two types of floating point numbers −

• Float − It can store single precision values of maximum 32 bits. We declare it using ‘float’ keyword.

• Double − Its size is comparatively larger than float and can store double precision values of maximum 64 bits. It is declared using ‘double’ keyword.

Let’s discuss an example in which we may encounter a rounding off error and then we will understand the reason and also provide the solution to handle it properly.

### Example 1

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);
}
}


### Output

Value after addition: 0.30000000000000004
false
Value after addition: 0.5
true


In the above code, the expected value of ‘data2’ where we performed the first addition is 0.30 but in the output we got wrong result. But, when we performed another similar addition we got the accurate result.

Let’s take another example that shows an error.

### Example 2

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);
}
}


### Output

Value after addition: 4.8080620000000005


The expected output is 4.808062 but we got wrong output here.

### Reason for errors

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 the reason why we are getting different values.

### Solution for the error

To avoid this rounding error, we have the following ways −

• 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.

### Example 3

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);
}
}


### Output

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 an argument and returns the nearest integer value. We call it using its class name.

The below example illustrates its use in the program.

### Example 4

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));
}
}


### Output

Value after addition: 4.8080620000000005
Value after rounding off the addition: 5


## Conclusion

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.

Updated on: 15-May-2023

277 Views

##### Kickstart Your Career

Get certified by completing the course

Advertisements