数学运算的意外结果
Unexpected results for mathematical operations
在执行数学运算为简单计算器开发 android 应用程序时,减法和除法运算产生了奇怪的结果。其他操作与提供的输入配合得很好。在我对精确处理的研究中,遇到了 BigDecimal class ,它减少了我的错误。
减法和除法等某些运算没有产生正确的结果。我在这里提供代码片段。
.
.
.
String answer = "";
BigDecimal bigDecimal1, bigDecimal2;
BigDecimal bigDecimalResult;
String operation = "";
.
.
.
case R.id.button_substraction:
bigDecimal1 = new BigDecimal(answer);
operation = "-";
answer = "";
textViewAnswer.setText(buttonSubstraction.getText().toString());
break;
case R.id.button_division:
bigDecimal1 = new BigDecimal(answer);
operation = "/";
answer = "";
textViewAnswer.setText(buttonDivision.getText().toString());
break;
case R.id.button_equal:
bigDecimal2 = new BigDecimal(answer);
answer = "";
switch (operation) {
.
.
case "-":
bigDecimalResult = (bigDecimal1.subtract(bigDecimal2)).setScale(6,RoundingMode.HALF_UP).stripTrailingZeros();
textViewAnswer.setText(bigDecimalResult.toString());
break;
case "/":
bigDecimalResult = (bigDecimal1.divide(bigDecimal2)).setScale(6,RoundingMode.HALF_UP).stripTrailingZeros();
textViewAnswer.setText(bigDecimalResult.toString());
break;
}
.
.
.
示例:
- 减法
当一个数字从自身中减去时,结果为 0,精度值为 6(在代码中设置)为,
100 - 100 = 0.000000
即使指定了 stripTrailingZeros()。
- 师
正在尝试分割
1 / 9
使我的应用程序崩溃。
- 我的逻辑实现有问题吗?
- 是否与内存中的存储表示有关,因为使用了 BigDecimal class?
您的 stripTrailingZeros()
未删除零值的问题已在 Java 8 中修复。
参见 JDK-6480539: BigDecimal.stripTrailingZeros() has no effect on zero itself ("0.0")
您的应用程序崩溃 1 / 9
问题是由您的逻辑引起的:
bigDecimalResult = bigDecimal1.divide(bigDecimal2)
.setScale(6, RoundingMode.HALF_UP)
.stripTrailingZeros()
您试图在除法后设置比例,但是1 / 9
没有确切的值,因此divide()
方法抛出异常,如 javadoc 中所述。
Throws ArithmeticException
if the exact quotient does not have a terminating decimal expansion
您应该调用 divide()
方法的其他重载之一:
bigDecimalResult = bigDecimal1.divide(bigDecimal2, 6, RoundingMode.HALF_UP)
.stripTrailingZeros()
现在,注意 stripTrailingZeros()
的结果。例如。如果结果是 100.00000
,stripTrailingZeros()
会从字面上去除 所有 的零,结果是 1E+2
。这可能不是你想要的。
要以更易于阅读的方式打印,您可能需要使用 toPlainString()
instead of toString()
。
在执行数学运算为简单计算器开发 android 应用程序时,减法和除法运算产生了奇怪的结果。其他操作与提供的输入配合得很好。在我对精确处理的研究中,遇到了 BigDecimal class ,它减少了我的错误。
减法和除法等某些运算没有产生正确的结果。我在这里提供代码片段。
.
.
.
String answer = "";
BigDecimal bigDecimal1, bigDecimal2;
BigDecimal bigDecimalResult;
String operation = "";
.
.
.
case R.id.button_substraction:
bigDecimal1 = new BigDecimal(answer);
operation = "-";
answer = "";
textViewAnswer.setText(buttonSubstraction.getText().toString());
break;
case R.id.button_division:
bigDecimal1 = new BigDecimal(answer);
operation = "/";
answer = "";
textViewAnswer.setText(buttonDivision.getText().toString());
break;
case R.id.button_equal:
bigDecimal2 = new BigDecimal(answer);
answer = "";
switch (operation) {
.
.
case "-":
bigDecimalResult = (bigDecimal1.subtract(bigDecimal2)).setScale(6,RoundingMode.HALF_UP).stripTrailingZeros();
textViewAnswer.setText(bigDecimalResult.toString());
break;
case "/":
bigDecimalResult = (bigDecimal1.divide(bigDecimal2)).setScale(6,RoundingMode.HALF_UP).stripTrailingZeros();
textViewAnswer.setText(bigDecimalResult.toString());
break;
}
.
.
.
示例:
- 减法
当一个数字从自身中减去时,结果为 0,精度值为 6(在代码中设置)为,
100 - 100 = 0.000000
即使指定了 stripTrailingZeros()。
- 师
正在尝试分割
1 / 9
使我的应用程序崩溃。
- 我的逻辑实现有问题吗?
- 是否与内存中的存储表示有关,因为使用了 BigDecimal class?
您的 stripTrailingZeros()
未删除零值的问题已在 Java 8 中修复。
参见 JDK-6480539: BigDecimal.stripTrailingZeros() has no effect on zero itself ("0.0")
您的应用程序崩溃 1 / 9
问题是由您的逻辑引起的:
bigDecimalResult = bigDecimal1.divide(bigDecimal2)
.setScale(6, RoundingMode.HALF_UP)
.stripTrailingZeros()
您试图在除法后设置比例,但是1 / 9
没有确切的值,因此divide()
方法抛出异常,如 javadoc 中所述。
Throws
ArithmeticException
if the exact quotient does not have a terminating decimal expansion
您应该调用 divide()
方法的其他重载之一:
bigDecimalResult = bigDecimal1.divide(bigDecimal2, 6, RoundingMode.HALF_UP)
.stripTrailingZeros()
现在,注意 stripTrailingZeros()
的结果。例如。如果结果是 100.00000
,stripTrailingZeros()
会从字面上去除 所有 的零,结果是 1E+2
。这可能不是你想要的。
要以更易于阅读的方式打印,您可能需要使用 toPlainString()
instead of toString()
。