首頁 >Java >java教程 >Java中的四捨五入誤差

Java中的四捨五入誤差

WBOY
WBOY轉載
2023-08-19 15:05:18690瀏覽

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 for may ressolution for ong ong a given mathematical problem. Generally, this error occurs when we work with floating point numbers.

在這篇文章中,我們將探討這個問題的原因,並試圖找到擺脫這種錯誤的方法。

舍入誤差

Floating Point Types

它們也被稱為實數,並且在計算需要分數值時使用。它代表具有小數點的數字。例如,表示1/5即0.2的結果,正弦和餘弦的結果也需要小數點。

在Java中,有兩種​​類型的浮點數 -

  • 浮點型 - 它可以儲存最大32位元的單一精確度值。我們使用 'float' 關鍵字來聲明它。

  • Double − 它的大小比float大,可以儲存最大64位元的雙精確值。它使用'double'關鍵字聲明。

讓我們討論一個例子,在這個例子中我們可能會遇到一個四捨五入誤差,然後我們將理解原因,並提供正確處理它的解決方案。

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

在上述程式碼中,我們在第一次相加時,‘data2’的預期值為0.30,但輸出結果是錯誤的。然而,當我們進行另一個類似的相加操作時,我們得到了準確的結果。

Let’s take another example that shows an error.

Example 2

的中文翻譯為:

範例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

預期輸出為4.808062,但我們這裡得到了錯誤的輸出。

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 23 123 23 bits then it adds 1 the 23bit the value. Here, the mantissa is the binary representation of fractional digits.

這就是我們得到不同值的原因。

解決錯誤的方法

為了避免這個捨入誤差,我們有以下幾種方法 -

  • 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() − 它是類別‘Math’的靜態方法。它以浮點數值作為參數,並傳回最接近的整數值。我們使用它的類別名稱來呼叫它。

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 have discussed the reason and also suggested some ways to overcome this rounding-off error.

以上是Java中的四捨五入誤差的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除