十进制vs双! - 我应该使用哪一个?何时使用? [重复]

时间:2021-07-23 06:04:16

This question already has an answer here:

这个问题在这里已有答案:

I keep seeing people using doubles in C#. I know I read somewhere that doubles sometimes lose precision. My question is when should a use a double and when should I use a decimal type? Which type is suitable for money computations? (ie. greater than $100 million)

我一直看到人们在C#中使用双打。我知道我读到某个地方,双打有时会失去精确度。我的问题是什么时候应该使用双倍,何时应该使用小数类型?哪种类型适合货币计算? (即超过1亿美元)

7 个解决方案

#1


For money, always decimal. It's why it was created.

为了钱,总是小数。这就是为什么它被创造出来的原因。

If numbers must add up correctly or balance, use decimal. This includes any financial storage or calculations, scores, or other numbers that people might do by hand.

如果数字必须正确加或平衡,请使用小数。这包括人们可能手工完成的任何财务存储或计算,分数或其他数字。

If the exact value of numbers is not important, use double for speed. This includes graphics, physics or other physical sciences computations where there is already a "number of significant digits".

如果数字的确切值不重要,请使用double表示速度。这包括图形,物理或其他物理科学计算,其中已经存在“有效位数”。

#2


My question is when should a use a double and when should I use a decimal type?

我的问题是什么时候应该使用双倍,何时应该使用小数类型?

decimal for when you work with values in the range of 10^(+/-28) and where you have expectations about the behaviour based on base 10 representations - basically money.

当你使用10 ^(+/- 28)范围内的值并且你对基于10的表示的行为有期望时的十进制 - 基本上是金钱。

double for when you need relative accuracy (i.e. losing precision in the trailing digits on large values is not a problem) across wildly different magnitudes - double covers more than 10^(+/-300). Scientific calculations are the best example here.

当你需要相对精确度(即在大值上的尾随数字中丢失精度不是一个问题)时,双倍的大小不同 - 双倍覆盖超过10 ^(+/- 300)。科学计算是这里最好的例子。

which type is suitable for money computations?

哪种类型适合货币计算?

decimal, decimal, decimal

十进制,十进制,十进制

Accept no substitutes.

不接受任何替代品。

The most important factor is that double, being implemented as a binary fraction, cannot accurately represent many decimal fractions (like 0.1) at all and its overall number of digits is smaller since it is 64-bit wide vs. 128-bit for decimal. Finally, financial applications often have to follow specific rounding modes (sometimes mandated by law). decimal supports these; double does not.

最重要的因素是,实现为二进制分数的double不能准确地表示许多小数部分(如0.1),并且其总位数较小,因为它是64位宽而小数位是128位。最后,财务应用程序通常必须遵循特定的舍入模式(有时是法律规定的)。小数支持这些;双重不。

#3


System.Single / float - 7 digits
System.Double / double - 15-16 digits
System.Decimal / decimal - 28-29 significant digits

System.Single / float - 7位数字System.Double / double - 15-16位数字System.Decimal / decimal - 28-29有效数字

The way I've been stung by using the wrong type (a good few years ago) is with large amounts:

我使用错误的类型(好几年前)被蜇的方式是大量的:

  • £520,532.52 - 8 digits
  • £520,532.52 - 8位数

  • £1,323,523.12 - 9 digits
  • £1,323,523.12 - 9位数

You run out at 1 million for a float.

你花了一百万用于浮动。

A 15 digit monetary value:

15位数的货币价值:

  • £1,234,567,890,123.45

9 trillion with a double. But with division and comparisons it's more complicated (I'm definitely no expert in floating point and irrational numbers - see Marc's point). Mixing decimals and doubles causes issues:

9万亿双倍。但是通过划分和比较它会更复杂(我绝对不是浮点数和无理数的专家 - 请参阅Marc的观点)。混合小数和双精度会导致问题:

A mathematical or comparison operation that uses a floating-point number might not yield the same result if a decimal number is used because the floating-point number might not exactly approximate the decimal number.

如果使用十进制数,使用浮点数的数学或比较操作可能不会产生相同的结果,因为浮点数可能不完全接近十进制数。

When should I use double instead of decimal? has some similar and more in depth answers.

什么时候应该使用double而不是decimal?有一些类似和更深入的答案。

Using double instead of decimal for monetary applications is a micro-optimization - that's the simplest way I look at it.

对货币应用程序使用double而不是decimal是一种微优化 - 这是我看待它的最简单方法。

#4


Decimal is for exact values. Double is for approximate values.

十进制是确切的值。 Double表示近似值。

USD: $12,345.67 USD (Decimal)
CAD: $13,617.27 (Decimal)
Exchange Rate: 1.102932 (Double)

#5


For money: decimal. It costs a little more memory, but doesn't have rounding troubles like double sometimes has.

对于钱:小数。它花费了更多的记忆,但没有像双倍的有时候的四舍五入的麻烦。

#6


Definitely use integer types for your money computations. This cannot be emphasized enough, since at first glance it might seem that a floating point type is adequate.

绝对使用整数类型进行货币计算。这一点不够强调,因为乍一看似乎浮点类型就足够了。

Here an example in python code:

这是python代码中的一个例子:

>>> amount = float(100.00) # one hundred dollars
>>> print amount
100.0
>>> new_amount = amount + 1
>>> print new_amount
101.0
>>> print new_amount - amount
>>> 1.0

looks pretty normal.

看起来很正常。

Now try this again with 10^20 Zimbabwe dollars

现在用10 ^ 20津巴布韦元再试一次

>>> amount = float(1e20)
>>> print amount
1e+20
>>> new_amount = amount + 1
>>> print new_amount
1e+20
>>> print new_amount-amount
0.0

As you can see, the dollar disappeared.

如你所见,美元消失了。

If you use the integer type, it works fine:

如果你使用整数类型,它工作正常:

>>> amount = int(1e20)
>>> print amount
100000000000000000000
>>> new_amount = amount + 1
>>> print new_amount
100000000000000000001
>>> print new_amount - amount
1

#7


I think that the main difference beside bit width is that decimal has exponent base 10 and double has 2

我认为位宽旁边的主要区别是十进制有指数基数10而双数有2

http://software-product-development.blogspot.com/2008/07/net-double-vs-decimal.html

#1


For money, always decimal. It's why it was created.

为了钱,总是小数。这就是为什么它被创造出来的原因。

If numbers must add up correctly or balance, use decimal. This includes any financial storage or calculations, scores, or other numbers that people might do by hand.

如果数字必须正确加或平衡,请使用小数。这包括人们可能手工完成的任何财务存储或计算,分数或其他数字。

If the exact value of numbers is not important, use double for speed. This includes graphics, physics or other physical sciences computations where there is already a "number of significant digits".

如果数字的确切值不重要,请使用double表示速度。这包括图形,物理或其他物理科学计算,其中已经存在“有效位数”。

#2


My question is when should a use a double and when should I use a decimal type?

我的问题是什么时候应该使用双倍,何时应该使用小数类型?

decimal for when you work with values in the range of 10^(+/-28) and where you have expectations about the behaviour based on base 10 representations - basically money.

当你使用10 ^(+/- 28)范围内的值并且你对基于10的表示的行为有期望时的十进制 - 基本上是金钱。

double for when you need relative accuracy (i.e. losing precision in the trailing digits on large values is not a problem) across wildly different magnitudes - double covers more than 10^(+/-300). Scientific calculations are the best example here.

当你需要相对精确度(即在大值上的尾随数字中丢失精度不是一个问题)时,双倍的大小不同 - 双倍覆盖超过10 ^(+/- 300)。科学计算是这里最好的例子。

which type is suitable for money computations?

哪种类型适合货币计算?

decimal, decimal, decimal

十进制,十进制,十进制

Accept no substitutes.

不接受任何替代品。

The most important factor is that double, being implemented as a binary fraction, cannot accurately represent many decimal fractions (like 0.1) at all and its overall number of digits is smaller since it is 64-bit wide vs. 128-bit for decimal. Finally, financial applications often have to follow specific rounding modes (sometimes mandated by law). decimal supports these; double does not.

最重要的因素是,实现为二进制分数的double不能准确地表示许多小数部分(如0.1),并且其总位数较小,因为它是64位宽而小数位是128位。最后,财务应用程序通常必须遵循特定的舍入模式(有时是法律规定的)。小数支持这些;双重不。

#3


System.Single / float - 7 digits
System.Double / double - 15-16 digits
System.Decimal / decimal - 28-29 significant digits

System.Single / float - 7位数字System.Double / double - 15-16位数字System.Decimal / decimal - 28-29有效数字

The way I've been stung by using the wrong type (a good few years ago) is with large amounts:

我使用错误的类型(好几年前)被蜇的方式是大量的:

  • £520,532.52 - 8 digits
  • £520,532.52 - 8位数

  • £1,323,523.12 - 9 digits
  • £1,323,523.12 - 9位数

You run out at 1 million for a float.

你花了一百万用于浮动。

A 15 digit monetary value:

15位数的货币价值:

  • £1,234,567,890,123.45

9 trillion with a double. But with division and comparisons it's more complicated (I'm definitely no expert in floating point and irrational numbers - see Marc's point). Mixing decimals and doubles causes issues:

9万亿双倍。但是通过划分和比较它会更复杂(我绝对不是浮点数和无理数的专家 - 请参阅Marc的观点)。混合小数和双精度会导致问题:

A mathematical or comparison operation that uses a floating-point number might not yield the same result if a decimal number is used because the floating-point number might not exactly approximate the decimal number.

如果使用十进制数,使用浮点数的数学或比较操作可能不会产生相同的结果,因为浮点数可能不完全接近十进制数。

When should I use double instead of decimal? has some similar and more in depth answers.

什么时候应该使用double而不是decimal?有一些类似和更深入的答案。

Using double instead of decimal for monetary applications is a micro-optimization - that's the simplest way I look at it.

对货币应用程序使用double而不是decimal是一种微优化 - 这是我看待它的最简单方法。

#4


Decimal is for exact values. Double is for approximate values.

十进制是确切的值。 Double表示近似值。

USD: $12,345.67 USD (Decimal)
CAD: $13,617.27 (Decimal)
Exchange Rate: 1.102932 (Double)

#5


For money: decimal. It costs a little more memory, but doesn't have rounding troubles like double sometimes has.

对于钱:小数。它花费了更多的记忆,但没有像双倍的有时候的四舍五入的麻烦。

#6


Definitely use integer types for your money computations. This cannot be emphasized enough, since at first glance it might seem that a floating point type is adequate.

绝对使用整数类型进行货币计算。这一点不够强调,因为乍一看似乎浮点类型就足够了。

Here an example in python code:

这是python代码中的一个例子:

>>> amount = float(100.00) # one hundred dollars
>>> print amount
100.0
>>> new_amount = amount + 1
>>> print new_amount
101.0
>>> print new_amount - amount
>>> 1.0

looks pretty normal.

看起来很正常。

Now try this again with 10^20 Zimbabwe dollars

现在用10 ^ 20津巴布韦元再试一次

>>> amount = float(1e20)
>>> print amount
1e+20
>>> new_amount = amount + 1
>>> print new_amount
1e+20
>>> print new_amount-amount
0.0

As you can see, the dollar disappeared.

如你所见,美元消失了。

If you use the integer type, it works fine:

如果你使用整数类型,它工作正常:

>>> amount = int(1e20)
>>> print amount
100000000000000000000
>>> new_amount = amount + 1
>>> print new_amount
100000000000000000001
>>> print new_amount - amount
1

#7


I think that the main difference beside bit width is that decimal has exponent base 10 and double has 2

我认为位宽旁边的主要区别是十进制有指数基数10而双数有2

http://software-product-development.blogspot.com/2008/07/net-double-vs-decimal.html