我什么时候应该使用双精度而不是十进制? [英] When should I use double instead of decimal?

查看:17
本文介绍了我什么时候应该使用双精度而不是十进制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以说出使用 double(或 float)代替 decimal 的三个优点:

I can name three advantages to using double (or float) instead of decimal:

  1. 使用更少的内存.
  2. 速度更快,因为处理器本身支持浮点数学运算.
  3. 可以表示更大范围的数字.

但这些优势似乎只适用于计算密集型操作,例如建模软件中的那些操作.当然,当需要精度时,例如财务计算,不应使用双精度数.那么在普通"应用程序中是否有任何实际理由选择 double(或 float)而不是 decimal?

But these advantages seem to apply only to calculation intensive operations, such as those found in modeling software. Of course, doubles should not be used when precision is required, such as financial calculations. So are there any practical reasons to ever choose double (or float) instead of decimal in "normal" applications?

编辑添加:感谢所有精彩的回复,我向他们学习.

Edited to add: Thanks for all the great responses, I learned from them.

另一个问题:有些人提出了双精度可以更精确地表示实数的观点.当声明时,我会认为它们通常也更准确地代表它们.但是,在执行浮点运算时精度可能会降低(有时会显着降低),这真的是真的吗?

One further question: A few people made the point that doubles can more precisely represent real numbers. When declared I would think that they usually more accurately represent them as well. But is it a true statement that the accuracy may decrease (sometimes significantly) when floating point operations are performed?

推荐答案

我认为您已经很好地总结了这些优点.然而,你错过了一分.decimal 类型只是更准确地表示基数 10 数字(例如用于货币/财务计算的数字).一般来说,doubletype 将至少提供同样高的精度(如果我错了,有人会纠正我),并且对于任意实数肯定会提供更快的速度.简单的结论是:在考虑使用哪个时,请始终使用 double,除非您需要 decimal 提供的 base 10 精度.

I think you've summarised the advantages quite well. You are however missing one point. The decimal type is only more accurate at representing base 10 numbers (e.g. those used in currency/financial calculations). In general, the double type is going to offer at least as great precision (someone correct me if I'm wrong) and definitely greater speed for arbitrary real numbers. The simple conclusion is: when considering which to use, always use double unless you need the base 10 accuracy that decimal offers.

关于您关于运算后浮点数精度降低的其他问题,这是一个稍微微妙的问题.事实上,在每次操作执行后,精度(我在这里交替使用这个术语来表示准确性)会稳步下降.这是由于两个原因:

Regarding your additional question about the decrease in accuracy of floating-point numbers after operations, this is a slightly more subtle issue. Indeed, precision (I use the term interchangeably for accuracy here) will steadily decrease after each operation is performed. This is due to two reasons:

  1. 某些数字(最明显的是小数)无法真正以浮点形式表示的事实
  2. 出现舍入错误,就像您手动进行计算一样.然而,这在很大程度上取决于上下文(您正在执行多少操作)这些错误是否严重到需要深思熟虑.

在所有情况下,如果您想比较两个理论上应该相等的浮点数(但使用不同的计算得出),您需要允许一定程度的容差(变化多少,但通常是很小).

In all cases, if you want to compare two floating-point numbers that should in theory be equivalent (but were arrived at using different calculations), you need to allow a certain degree of tolerance (how much varies, but is typically very small).

有关可以引入精度误差的特定情况的更详细概述,请参阅维基百科文章.最后,如果您想在机器级别对浮点数/运算进行深入(和数学)的讨论,请尝试阅读经常引用的文章 每个计算机科学家都应该了解的关于浮点运算的知识.

For a more detailed overview of the particular cases where errors in accuracies can be introduced, see the Accuracy section of the Wikipedia article. Finally, if you want a seriously in-depth (and mathematical) discussion of floating-point numbers/operations at machine level, try reading the oft-quoted article What Every Computer Scientist Should Know About Floating-Point Arithmetic.

这篇关于我什么时候应该使用双精度而不是十进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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