飞镖双精度 [英] Dart double division precision

查看:42
本文介绍了飞镖双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行此操作的正确方法是什么?

What is the correct way to perform this operation?

399.9 / 100

我希望看到的是

3.999

但结果是

3.9989999999999997

推荐答案

您看到的结果是正确,这不是您想要的.

The result you see is correct, it's just not what you want.

双精度不是精确值.通过写入399.9获得的双精度值实际上是精确值.

Doubles are not precise values. The double you get by writing 399.9 is actually the precise value.

399.8999999999999772626324556556940521240234375

399.8999999999999772626324556767940521240234375

这是最接近精确值399.9的双精度值.其他任何两倍都至少离399.9远.

That's the closest available double to the exact value 399.9. Any other double is at least as far away from 399.9 as that.

然后您将其除以100.同样,结果也不精确,但是最接近的double具有确切的值

Then you divide by 100. Again, the result is not precise, but the closest double has the exact value

3.99899999999999966604491419275291264057159423828125

3.99899999999999966604491419275291264057159423828125

这与通过编写3.999得到的结果有所不同,这是确切的值:

That differs from what you would get by writing 3.999, which is the exact value:

3.99900000000000011011012124042815528810024261474609375

3.999000000000000110134124042815528810024261474609375

在每个步骤中,两次运算都会使错误最小化,但是由于您要执行多个步骤,因此最终结果与最接近数学结果的两次运算结果有所出入.

At every step, the double operations have minimized the error, but because you are doing multiple steps, the final result diverges from the double closest to the mathematical result.

您需要做什么取决于您的实际需求.

What you need to do depends on what your actual requirements are.

如果您总是要用两个有效数字进行计算,那么我只需将数字乘以100,然后将所有运算作为整数运算,直到最后除以100.

If you want to always calculate with two significant digits, then I'd just multiply my numbers with 100 and do all the operations as integer operations, until the very last division by 100.

如果您有一个中间结果,并且想将其四舍五入为两位数,那么我将按照Fy Z1K的说明进行操作:

If you have an intermediate result and wants to round it to two digits, I'd do what Fy Z1K says:

  result = (result * 100).round() / 100;

这篇关于飞镖双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆