作者:小葵小小葵_530 | 来源:互联网 | 2022-12-24 17:39
Ihavecodetocalculatethepercentagedifferencebetween2numbers-(oldNum-newNum)oldNum*
I have code to calculate the percentage difference between 2 numbers - (oldNum - newNum) / oldNum * 100;
- where both of the numbers are double
s. I expected to have to add some sort of checking / exception handling in case oldNum is 0. However, when I did a test run with values of 0.0 for both oldNum and newNum, execution continued as if nothing had happened and no error was thrown. Running this code with int
s would definitely cause an arithmetic division-by-zero exception. Why does Java ignore it when it comes to double
s?
我有代码来计算2个数字之间的百分比差异 - (oldNum - newNum)/ oldNum * 100; - 两个数字都是双打的。我希望在oldNum为0的情况下添加某种检查/异常处理。但是,当我为oldNum和newNum进行了值为0.0的测试运行时,执行继续,好像什么都没发生并且没有抛出错误。使用int运行此代码肯定会导致算术除零异常。为什么Java在双打时会忽略它?
5 个解决方案