为什么是System.Math,例如MathNet.Numerics基于double? [英] Why is System.Math and for example MathNet.Numerics based on double?

查看:134
本文介绍了为什么是System.Math,例如MathNet.Numerics基于double?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Math 中的所有方法都需要code> double 作为参数并返回参数。常量也是类型 double 。我查看了 MathNet.Numerics ,同样的情况也是如此。



为什么是这样?特别是常数。不是 decimal 应该更准确吗?在做计算时经常会有用吗?

解决方案

这是一个经典的速度与准确性的折衷。但是,请注意,对于PI,例如,您将需要的最多数字是41。



< blockquote>

您将需要的pi
的最大位数是41.对
计算
的圆周长度,错误小于
质子的直径,您需要41
位数的pi†。
似乎是安全的结论,41位数足够
的准确性在任何圈子
测量问题你可能会
遇到。因此,在
2002中计算的超过一个
万亿位数的pi中,除第41个之外的所有数字都有
没有实际价值。


此外,十进制和二进制的内部存储结构略有不同。小数被设计为存储基础10数据,其中双精度(和浮点数)被保存为二进制数据。在二进制机器上(如现有的每台计算机),在存储任何数量的范围内时,双倍的数据将会减少。



还要考虑:

 System.Double 8个字节大约±5.0e-324到±1.7e308,15或16个有效数字
系统最小12个字节约±1.0e-28到± 7.9e28有28或29个有效数字

正如你所见,十进制的范围更小,但精度更高。


All the methods in System.Math takes double as parameters and returns parameters. The constants are also of type double. I checked out MathNet.Numerics, and the same seems to be the case there.

Why is this? Especially for constants. Isn't decimal supposed to be more exact? Wouldn't that often be kind of useful when doing calculations?

解决方案

This is a classic speed-versus-accuracy trade off.

However, keep in mind that for PI, for example, the most digits you will ever need is 41.

The largest number of digits of pi that you will ever need is 41. To compute the circumference of the universe with an error less than the diameter of a proton, you need 41 digits of pi †. It seems safe to conclude that 41 digits is sufficient accuracy in pi for any circle measurement problem you're likely to encounter. Thus, in the over one trillion digits of pi computed in 2002, all digits beyond the 41st have no practical value.

In addition, decimal and double have a slightly different internal storage structure. Decimals are designed to store base 10 data, where as doubles (and floats), are made to hold binary data. On a binary machine (like every computer in existence) a double will have fewer wasted bits when storing any number within its range.

Also consider:

System.Double      8 bytes    Approximately ±5.0e-324 to ±1.7e308 with 15 or 16 significant figures
System.Decimal    12 bytes    Approximately ±1.0e-28 to ±7.9e28 with 28 or 29 significant figures

As you can see, decimal has a smaller range, but a higher precision.

这篇关于为什么是System.Math,例如MathNet.Numerics基于double?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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