Home > Java > javaTutorial > Rounding error in Java

Rounding error in Java

WBOY
Release: 2023-08-19 15:05:18
forward
684 people have browsed it

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

Rounding error

Floating Point Types

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.

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);
   }
} 
Copy after login

Output

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

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 of

Example 2

is:

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);
   }
}
Copy after login

Output

Value after addition: 4.8080620000000005
Copy after login

The expected output is 4.808062, but we get the 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 why we get different values.

How to solve the error

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.

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);
   }
}
Copy after login

Output

Value after addition: 0.30
Copy after login

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.

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));
   }
}
Copy after login

Output

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

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.

The above is the detailed content of Rounding error in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template