小数与双速 [英] Decimal vs Double Speed

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

问题描述

我编写财务应用程序,我不断地决定使用双倍与使用小数。

I write financial applications where I constantly battle the decision to use a double vs using a decimal.

我的所有数学作品都不超过5位数地方不超过〜100,000。我有一种感觉,所有这些都可以表示为双打,而没有舍入误差,但从来没有确定。

All of my math works on numbers with no more than 5 decimal places and are not larger than ~100,000. I have a feeling that all of these can be represented as doubles anyways without rounding error, but have never been sure.

我会继续前进,从小数切换到除了在一天结束的时候,我仍然使用ToString方法将价格传递给交易所,并且需要确保它总是输出我期望的数字,这是显而易见的速度优势。 (89.99,而不是89.99000000001)

I would go ahead and make the switch from decimals to doubles for the obvious speed advantage, except that at the end of the day, I still use the ToString method to transmit prices to exchanges, and need to make sure it always outputs the number I expect. (89.99 instead of 89.99000000001)

问题:


  1. 像天真的测试一样大吗? (〜100次)

  2. 有没有办法保证ToString的输出成为我想要的?这个事实证明我的号码总是可以代表吗?

更新:在我的应用程序之前,我必须处理大约100亿的价格更新可以运行,我现在已经实施了十进制,出于明显的保护原因,但只需要3个小时即可打开,双打会大大减少我的时间。有没有一个安全的方式来做双打?

UPDATE: I have to process ~ 10 billion price updates before my app can run, and I have implemented with decimal right now for the obvious protective reasons, but it takes ~3 hours just to turn on, doubles would dramatically reduce my turn on time. Is there a safe way to do it with doubles?

推荐答案


  1. 浮点运算几乎总是显着更快,因为它是由硬件直接支持的。到目前为止,几乎没有广泛使用的硬件支持十进制算术(尽管这正在改变,见注释)。

  2. 金融应用程序应该总是使用十进制数字,恐怖数量在金融应用中使用浮点的故事是无止境的,您应该能够通过Google搜索找到许多此类示例。

  3. 尽管十进制算术可能比浮点运算慢得多,除非您花费大量时间来处理十进制数据对您的程序的影响可能是微不足道的。一如往常,做适当的分析,然后再开始担心差异。

  1. Floating point arithmetic will almost always be significantly faster because it is supported directly by the hardware. So far almost no widely used hardware supports decimal arithmetic (although this is changing, see comments).
  2. Financial applications should always use decimal numbers, the number of horror stories stemming from using floating point in financial applications is endless, you should be able to find many such examples with a Google search.
  3. While decimal arithmetic may be significantly slower than floating point arithmetic, unless you are spending a significant amount of time processing decimal data the impact on your program is likely to be negligible. As always, do the appropriate profiling before you start worrying about the difference.

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

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