In this sample code
在此示例代码中
double number = 2646849856355434345;
Console.WriteLine( number % 5 );
If I use Double for this operation, it won't give a precise answer. Here it will output "1" where it should output "0".
如果我使用Double进行此操作,则无法给出准确的答案。这里输出“1”,输出“0”。
Is there anyway to increase the precision for large double numbers ?
反正有没有提高大双数的精度?
Note: I know about BigInteger and I can use them but BigInteger takes long time and I'm wondering If I can have the same precision with doubles.
注意:我知道BigInteger并且我可以使用它们,但BigInteger需要很长时间,我想知道如果我可以与双打具有相同的精度。
2 个解决方案
#1
3
You can use Decimal.
你可以使用十进制。
It is suitable for financial and monetary calculations. Double is based on IEEE Standard for Floating-Point Arithmetic (IEEE 754).
它适用于财务和货币计算。 Double基于IEEE浮点运算标准(IEEE 754)。
If you want to know the differences between binary floating point and decimal floating point read the following articles (reference).
如果您想知道二进制浮点和十进制浮点之间的差异,请阅读以下文章(参考)。
Binary floating point (float/double)
Decimal floating point (decimal)
二进制浮点(float / double)十进制浮点(十进制)
#2
1
Double
should not be used for operations that require decimal precision you should use the Decimal data type for decimal arithmetic.
Double不应该用于需要小数精度的操作,您应该使用Decimal数据类型进行十进制算术运算。
The Double
data type is precise only for binary arithmetic, being based on binary floating points it is impossible for it to be 100% precise for decimal arithmetic.
Double数据类型仅对二进制算术是精确的,基于二进制浮点,它对于十进制算术是不可能100%精确的。
#1
3
You can use Decimal.
你可以使用十进制。
It is suitable for financial and monetary calculations. Double is based on IEEE Standard for Floating-Point Arithmetic (IEEE 754).
它适用于财务和货币计算。 Double基于IEEE浮点运算标准(IEEE 754)。
If you want to know the differences between binary floating point and decimal floating point read the following articles (reference).
如果您想知道二进制浮点和十进制浮点之间的差异,请阅读以下文章(参考)。
Binary floating point (float/double)
Decimal floating point (decimal)
二进制浮点(float / double)十进制浮点(十进制)
#2
1
Double
should not be used for operations that require decimal precision you should use the Decimal data type for decimal arithmetic.
Double不应该用于需要小数精度的操作,您应该使用Decimal数据类型进行十进制算术运算。
The Double
data type is precise only for binary arithmetic, being based on binary floating points it is impossible for it to be 100% precise for decimal arithmetic.
Double数据类型仅对二进制算术是精确的,基于二进制浮点,它对于十进制算术是不可能100%精确的。