什么时候应该使用双代替小数? [英] When should I use double instead of decimal?

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

问题描述

我可以说出三个优点,使用双击(或浮动)代替十进制

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


  1. 使用更少的内存。

  2. 更快,因为浮点数学运算的原生由处理器的支持。

  3. 可以重新present号码范围较大。

但是,这些优点似乎只适用于计算密集的操作,如在建模软件中找到。当然,当需要precision,如金融计算双打不应使用。那么,有没有任何现实的原因永远选择双击(或浮动)代替小数在正常的应用程序?

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.

另外一个问题:一些人提出,双打可以更precisely重新present实数点。当宣布我想,他们通常更准确地重新present他们。但是这是一个真正的声明中表示,精确度可能会降低(有时显著)执行浮点运算的时候?

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?

推荐答案

我想你总结的优点相当不错。然而,你缺少一个点。该 小数 类型仅在重新presenting更准确的基地10 的数字(例如那些货币/金融计算中使用)。一般来说, 双击 类型是要提供至少一样大precision(有人纠正我,如果我错了),肯定更快的速度为任意实数。简单的结论是:考虑使用哪一个时,总是用双击除非你需要在基地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.

编辑:

关于你提到的有关在手术后浮点数的精度降低额外问题,这是一个稍微更微妙的问题。事实上,precision(我用这个词互换的准确性这里)将稳步下降执行每个操作之后。这是由于两个原因:一)的事实,某些号码(最明显的是小数)不能在浮点形式psented真正重新$ P $,B)发生舍入误差,就好像你在做手工计算。这在很大程度上取决于上下文(正在执行多少操作)这些错误是否足以显著不过,值得多想。在任何情况下,如果你想比较一下在理论上应该等于(但使用不同的计算方式算出的)两个浮点数字,你需要允许一定程度的宽容(多少有所不同,但通常很小)

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: a) the fact that certain numbers (most obviously decimals) can't be truly represented in floating point form, b) rounding errors occur, just as if you were doing the calculation by hand. It depends greatly on the context (how many operations you're performing) whether these errors are significant enough to warrant much thought however. 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天全站免登陆