我在 C# 中使用 Math.Ceiling() 时遇到了一些问题。
我将其称为除以一个数字,然后乘以相同数字的结果,例如20 000/184 * 184。我预计结果是 20 000,但实际结果是 20 001。在尝试向上取整值时,是否有任何可能的方法可以避免这种行为?
提前谢谢
最佳答案
运行您提供的代码时,我们有以下内容
20000/oneEightyFour * oneEightyFour
答案是20000.000000000000000000000001
因此,当您制作上限时,我们有 20001
。
通过下面的文章,我认为结果是由于执行除法时引入的不准确,这会产生108.69565217391304347826086957
,正如乔恩所说
As a very broad rule of thumb, if you end up seeing a very long string representation (ie most of the 28/29 digits are non-zero) then chances are you've got some inaccuracy along the way.
关于c# - 除法和乘以相同数字时表达式上的 Math.Ceiling(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31923380/