[英]How does using decimal for money avoid the floating point problems in python?
So currency/money has lot's of known math issues when using a floating point.因此,在使用浮点数时,货币/货币有很多已知的数学问题。 It seems in python that decimal
is used in money libraries, but according to the python docs , decimal
is based on a floating point.在 python 中,货币库中使用了decimal
,但根据python 文档, decimal
基于浮点数。 So how does this not have the same problems?那么这怎么没有同样的问题呢?
context a lot of currency libraries measure their monetary values as integers (so cents of USD, not dollars).上下文许多货币库以整数来衡量它们的货币价值(因此是美分,而不是美元)。 We've just had the issue of a python application representing it's money as decimal, it goes into javascript, which then needs to convert it to an integer for another service.我们刚刚遇到了一个 python 应用程序的问题,它用十进制表示它的钱,它进入 javascript,然后需要将它转换为另一个服务的整数。
10.05 / 100, became 1050.0000...1 which is of course, not an integer. 10.05 / 100,变成了 1050.0000...1 这当然不是整数。 So of course I was wondering why python chose this route, as most recommendations I've seen recommend treating money as integers.所以我当然想知道为什么 python 选择这条路线,因为我见过的大多数建议都建议将钱视为整数。
You are confusing binary floating point with decimal floating point.您将二进制浮点数与十进制浮点数混淆。 From the module documentation :从模块文档:
The decimal module provides support for fast correctly-rounded decimal floating point arithmetic.十进制模块为快速正确舍入的十进制浮点运算提供支持。
[...] [...]
- Decimal numbers can be represented exactly.十进制数可以精确表示。 In contrast, numbers like 1.1 and 2.2 do not have exact representations in binary floating point相比之下,像 1.1 和 2.2 这样的数字在二进制浮点数中没有精确的表示
(bold emphasis mine). (粗体强调我的)。
The floating point aspect refers to the variability of the exponent;浮点方面是指指数的可变性; the number 12300000
can be represented as 123 with a decimal exponent of 5 (10 ** 5).数字12300000
可以表示为 123,十进制指数为 5 (10 ** 5)。 Both float
and decimal
use a floating point representation. float
和decimal
使用浮点数表示。 But float
adds up a number of binary fractions (1/2 + 1/4 + 1/8 + 1/16 + ...), and that makes them unsuitable for representing currencies as binary fractions can not predicisely model 1/100ths or 1/10ths, which currency values tend to use a lot.但是float
加起来了许多二进制分数(1/2 + 1/4 + 1/8 + 1/16 + ...),这使得它们不适合表示货币,因为二进制分数不能精确地模拟 1/100 或1/10,这些货币价值往往使用很多。
The DZone article on floating point issues for currency you link also teaches you about the Java java.math.BigDecimal
package .您链接的有关货币浮点问题的DZone 文章还教您有关 Java java.math.BigDecimal
包的知识。 Python's decimal
is essentially the same thing ; Python 的decimal
本质上是一回事; where the BigDecimal
documentation talks about values consist[ing] of an arbitrary precision integer unscaled value and a 32-bit integer scale , the scale is essentially the position of the floating point. BigDecimal
文档讨论的值由 [ing] 任意精度整数未缩放值和 32 位整数 scale 组成,其中scale本质上是浮点的位置。
Because decimal
can represent 1/100ths (cents) in currency values exactly, it is far more suitable to model currency values.因为decimal
可以decimal
地表示货币价值的 1/100(美分),所以它更适合对货币价值建模。
Decimal avoids some of the problems of binary floating-point, but not all, possibly not even most. Decimal 避免了二进制浮点的一些问题,但不是全部,甚至可能不是大部分。
The actual problem is not floating-point but numerical formats.实际问题不是浮点而是数字格式。 No numerical format can represent all real numbers, or even all rational numbers, so no numerical format can handle all the operations we want to do with numbers.没有数字格式可以表示所有实数,甚至所有有理数,因此没有数字格式可以处理我们想要对数字进行的所有操作。
Money is commonly represented in decimal fractions of a unit of currency.货币通常以货币单位的十进制小数表示。 For example, the US dollar and many other currencies have as a “cent” which is 1/100 th of a dollar.例如,美元和其他许多货币都有一个“分”,这是1/100美元。 A decimal format can represent 1/100 th exactly.十进制格式可以精确地表示1/100。 A binary format cannot.二进制格式不能。 So, with a decimal format, you can:因此,使用十进制格式,您可以:
However, problems arise when you try:但是,当您尝试时会出现问题:
There is no general solution to how to compute numerically.对于如何进行数值计算,没有通用的解决方案。 Studying numerical computing and its errors is an entire field of study with textbooks, courses, and research papers.研究数值计算及其错误是一个完整的研究领域,包括教科书、课程和研究论文。 So you cannot solve numerical problems merely by choosing a format.所以你不能仅仅通过选择一种格式来解决数值问题。 It is important to understand whatever format(s) you use, what errors arise in using them, how to deal with those errors, and what results you need to achieve.了解您使用的任何格式、使用它们时出现的错误、如何处理这些错误以及您需要达到的结果是很重要的。
Decimal types allow decimal floating point rather than binary floating point.十进制类型允许十进制浮点而不是二进制浮点。 The class of problems you are referring to relate to the latter.您所指的问题类别与后者有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.