on-terminating decimal expansion; no exact representable decimal result.
时间: 2023-10-31 19:39:12 浏览: 40
An irrational number has a non-terminating decimal expansion that cannot be written as a finite or repeating decimal. This means that the decimal representation of an irrational number goes on forever without repeating any pattern. Examples of irrational numbers include √2, π, and e. Because their decimal expansions are non-terminating and non-repeating, they cannot be expressed as an exact representable decimal result. Instead, they are usually approximated using a finite number of decimal places.
相关问题
Non-terminating decimal expansion; no exact representable decimal result.
在进行浮点数运算或者使用BigDecimal进行除法运算时,有些数无法被精确表示,因为它们的十进制小数位是无限循环的,例如1/3,0.1等。在这种情况下,运算结果也无法被精确表示,因为它们的十进制小数位也会是无限循环的。
在Java中,如果进行了这种运算,会抛出`ArithmeticException`异常,异常信息为"Non-terminating decimal expansion; no exact representable decimal result.",表示运算结果无法被精确表示为一个有限小数。
例如,下面的代码会抛出该异常:
```java
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
BigDecimal dividend = new BigDecimal("1");
BigDecimal divisor = new BigDecimal("3");
BigDecimal result = dividend.divide(divisor);
System.out.println(result);
}
}
```
输出结果为:
```
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1697)
at Main.main(Main.java:6)
```
要解决这个问题,可以使用`setScale()`方法将结果舍入到指定的小数位数,例如:
```java
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Main {
public static void main(String[] args) {
BigDecimal dividend = new BigDecimal("1");
BigDecimal divisor = new BigDecimal("3");
BigDecimal result = dividend.divide(divisor, 3, RoundingMode.HALF_UP);
System.out.println(result);
}
}
```
输出结果为:
```
0.333
```
在这个例子中,我们使用`setScale()`方法将结果舍入到小数点后3位,并使用`RoundingMode.HALF_UP`模式进行舍入,得到了一个近似值。
Non-terminating decimal expansion; no exact representable decimal result的处理
当使用BigDecimal进行精确计算时,如果出现Non-terminating decimal expansion; no exact representable decimal result异常,可以通过指定舍入模式来解决。舍入模式可以在调用divide方法时作为参数传递。常见的舍入模式有:
1.ROUND_UP:向远离零的方向舍入。
2.ROUND_DOWN:向接近零的方向舍入。
3.ROUND_CEILING:向正无穷方向舍入。
4.ROUND_FLOOR:向负无穷方向舍入。
5.ROUND_HALF_UP:四舍五入,如果舍弃部分>= 0.5,则向远离零的方向舍入。
6.ROUND_HALF_DOWN:四舍五入,如果舍弃部分> 0.5,则向远离零的方向舍入。
7.ROUND_HALF_EVEN:如果舍弃部分左边的数字为偶数,则向远离零的方向舍入;否则,向接近零的方向舍入。
8.ROUND_UNNECESSARY:如果存在非精确结果,则抛出ArithmeticException。
例如,我们可以使用ROUND_HALF_UP舍入模式来解决Non-terminating decimal expansion; no exact representable decimal result异常:
```java
BigDecimal a = new BigDecimal("1");
BigDecimal b = new BigDecimal("3");
BigDecimal c = a.divide(b, 10, RoundingMode.HALF_UP);
System.out.println(c);
```
上述代码将1除以3,并将结果舍入到小数点后10位,使用ROUND_HALF_UP舍入模式。这样就可以避免Non-terminating decimal expansion; no exact representable decimal result异常的出现。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)