python Decimal.as_integer_ratio() 输出
python Decimal.as_integer_ratio() output
在标准库中学习Decimal,这似乎是python 3.6中的新功能:
as_integer_ratio()
Return 一对 (n, d) 整数,将给定的 Decimal 实例表示为分数,以最小项表示并具有正分母
所以我试了一下:
print(Decimal(1.25).as_integer_ratio()) # >>> (5, 4)
print(Decimal(1.33).as_integer_ratio()) # >>> (748723438050345, 562949953421312)
对 1.25
工作正常,但很难相信 748723438050345 / 562949953421312
是 1.33
的最低分数项,也许 133/100
会更好输出?
好的,我得到了关于使用 Decimal('1.33')
而不是 Decimal(1.33)
的回答,所以这是否意味着在使用 Decimal( )?
尝试使用
from decimal import Decimal
print(Decimal('1.25').as_integer_ratio()) # >>> (5, 4)
print(Decimal('1.33').as_integer_ratio())
(5, 4)
(133, 100)
与标准的区别
当你这样做时
Decimal(1.25)
Python 将我们的文字基数 10 表示转换为浮点对象中的内部基数 2 表示。我们选择的两个值都不能精确地以 2 为基数表示,因此会进行一些舍入。然后将这些四舍五入的浮点值传递给 decimal 构造函数,并用于构造内部以 10 为基数的表示形式,这些表示形式将用于计算。最后对小数对象进行操作。因此,尽管 decimal 构造函数支持从 float 转换,但您应该始终将小数 decimal 文字指定为字符串,以避免创建不精确的中间 base 2 float 对象
为防止此类事故,请设置
To avoid inadvertently constructing decimal objects from floats, we
can modify the signal handling in the decimal module. We do this by
setting to true the value associated with the float operation key in
the traps attribute of the decimal module context. With this change in
place, our attempt to construct decimal objects directly from floats
raises a FloatOperation exception.
decimal.getcontext().traps[decimal.FloatOperation] = True
例如,对于以下情况,将引发异常
Decimal(0.8) > 0.7
回答你的问题
Decimal(1.33), so does that mean it is preferred to input str other than float when using Decimal()
Python float 类型即使是最简单的十进制值也会导致问题,这在任何需要精确算术的应用程序中都是不可接受的,例如在金融环境中。 Decimal 类型是一种快速正确舍入的数字类型,用于以 10 为基数执行算术运算。重要的是,decimal 类型仍然是浮点类型,尽管它的基数是 10 而不是 2,并且具有有限的精度,尽管用户可以配置而不是固定的。对于会计应用程序,使用小数代替浮点数可以显着减少难以调试的边缘情况。
Decimal 包的创建是为了消除 float 的不足。
你传入的是 float 对象,是那些引入了不精确的对象,你不能用二进制分数表示 1 和 1/3:
>>> format(1.33, '.53')
'1.3300000000000000710542735760100185871124267578125'
>>> (1.33).as_integer_ratio()
(748723438050345, 562949953421312)
改为传入 字符串 :
>>> Decimal('1.33').as_integer_ratio()
(133, 100)
来自文档:
Decimal numbers can be represented exactly. In contrast, numbers like 1.1
and 2.2
do not have exact representations in binary floating point. End users typically would not expect 1.1 + 2.2
to display as 3.3000000000000003
as it does with binary floating point.
和
If value is a float
, the binary floating point value is losslessly converted to its exact decimal equivalent. This conversion can often require 53 or more digits of precision. For example, Decimal(float('1.1'))
converts to Decimal('1.100000000000000088817841970012523233890533447265625')
.
在标准库中学习Decimal,这似乎是python 3.6中的新功能:
as_integer_ratio()
Return 一对 (n, d) 整数,将给定的 Decimal 实例表示为分数,以最小项表示并具有正分母
所以我试了一下:
print(Decimal(1.25).as_integer_ratio()) # >>> (5, 4)
print(Decimal(1.33).as_integer_ratio()) # >>> (748723438050345, 562949953421312)
对 1.25
工作正常,但很难相信 748723438050345 / 562949953421312
是 1.33
的最低分数项,也许 133/100
会更好输出?
好的,我得到了关于使用 Decimal('1.33')
而不是 Decimal(1.33)
的回答,所以这是否意味着在使用 Decimal( )?
尝试使用
from decimal import Decimal
print(Decimal('1.25').as_integer_ratio()) # >>> (5, 4)
print(Decimal('1.33').as_integer_ratio())
(5, 4)
(133, 100)
与标准的区别
当你这样做时
Decimal(1.25)
Python 将我们的文字基数 10 表示转换为浮点对象中的内部基数 2 表示。我们选择的两个值都不能精确地以 2 为基数表示,因此会进行一些舍入。然后将这些四舍五入的浮点值传递给 decimal 构造函数,并用于构造内部以 10 为基数的表示形式,这些表示形式将用于计算。最后对小数对象进行操作。因此,尽管 decimal 构造函数支持从 float 转换,但您应该始终将小数 decimal 文字指定为字符串,以避免创建不精确的中间 base 2 float 对象
为防止此类事故,请设置
To avoid inadvertently constructing decimal objects from floats, we can modify the signal handling in the decimal module. We do this by setting to true the value associated with the float operation key in the traps attribute of the decimal module context. With this change in place, our attempt to construct decimal objects directly from floats raises a FloatOperation exception.
decimal.getcontext().traps[decimal.FloatOperation] = True
例如,对于以下情况,将引发异常
Decimal(0.8) > 0.7
回答你的问题
Decimal(1.33), so does that mean it is preferred to input str other than float when using Decimal()
Python float 类型即使是最简单的十进制值也会导致问题,这在任何需要精确算术的应用程序中都是不可接受的,例如在金融环境中。 Decimal 类型是一种快速正确舍入的数字类型,用于以 10 为基数执行算术运算。重要的是,decimal 类型仍然是浮点类型,尽管它的基数是 10 而不是 2,并且具有有限的精度,尽管用户可以配置而不是固定的。对于会计应用程序,使用小数代替浮点数可以显着减少难以调试的边缘情况。 Decimal 包的创建是为了消除 float 的不足。
你传入的是 float 对象,是那些引入了不精确的对象,你不能用二进制分数表示 1 和 1/3:
>>> format(1.33, '.53')
'1.3300000000000000710542735760100185871124267578125'
>>> (1.33).as_integer_ratio()
(748723438050345, 562949953421312)
改为传入 字符串 :
>>> Decimal('1.33').as_integer_ratio()
(133, 100)
来自文档:
Decimal numbers can be represented exactly. In contrast, numbers like
1.1
and2.2
do not have exact representations in binary floating point. End users typically would not expect1.1 + 2.2
to display as3.3000000000000003
as it does with binary floating point.
和
If value is a
float
, the binary floating point value is losslessly converted to its exact decimal equivalent. This conversion can often require 53 or more digits of precision. For example,Decimal(float('1.1'))
converts toDecimal('1.100000000000000088817841970012523233890533447265625')
.