能比双C#存储更精确的数据? [英] Can C# store more precise data than doubles?

查看:113
本文介绍了能比双C#存储更精确的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

双击在C#中没有为我的需要保持足够的精度。我写一个分形程序,并在几次我跑出来的精密变焦后。

double in C# don't hold enough precision for my needs. I am writing a fractal program, and after zooming in a few times I run out of precision.

我有可以容纳更精确的浮点信息(即小数位),比双数据类型?

I there a data type that can hold more precise floating-point information (i.e more decimal places) than a double?

推荐答案

是的, 小数 是专为这一点。

Yes, decimal is designed for just that.

不过,千万要注意小数类型的范围较小比双。这就是双能容纳较大的值,但它因失去精度这样做。或者,在MSDN上的所述:

However, do be aware that the range of the decimal type is smaller than a double. That is double can hold a larger value, but it does so by losing precision. Or, as stated on MSDN:

十进制关键字表示128位
的数据类型。相比浮点
类型,小数类型具有更大
的精度和更小的范围内,这
使得它适合于金融和
货币计算。为十进制
型的近似
范围和精度示于下表中

The decimal keyword denotes a 128-bit data type. Compared to floating-point types, the decimal type has a greater precision and a smaller range, which makes it suitable for financial and monetary calculations. The approximate range and precision for the decimal type are shown in the following table.

的主要区别之间的小数双击是的 小数的定点和双击是浮点。这意味着,小数存储一个准确的值,而表示由分数表示的值,并且是不太精确的。 A 小数为128位,所以它需要的双重空间来存储。在小数计算也比较慢(测量!)。

The primary difference between decimal and double is that decimal is fixed-point and double is floating point. That means that decimal stores an exact value, while double represents a value represented by a fraction, and is less precise. A decimalis 128 bits, so it takes the double space to store. Calculations on decimal is also slower (measure !).

如果你需要更大的精度,那么的BigInteger 可以从.NET 4中使用(您将需要处理小数点自己)。在这里,你应该知道,这BigInteger的是不变的,所以它的所有运算将创建一个新的实例 - 如果数字很大,这可能是沉重的性能。

If you need even larger precision, then BigInteger can be used from .NET 4. (You will need to handle decimal points yourself). Here you should be aware, that BigInteger is immutable, so any arithmetic operation on it will create a new instance - if numbers are large, this might be crippling for performance.

我建议你​​看看你需要究竟是如何准确是。也许你的算法可以与标准值工作,这可以更小?如果性能是一个问题时,内置在浮点类型很可能是更快之一

I suggest you look into exactly how precise you need to be. Perhaps your algorithm can work with normalized values, that can be smaller ? If performance is an issue, one of the built in floating point types are likely to be faster.

这篇关于能比双C#存储更精确的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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