将十进制数添加到在python中不能正常工作的十进制数[复制]

时间:2022-12-16 12:16:34

This question already has an answer here:

这个问题已经有了答案:

I'm trying to add decimal numbers a decimal number and it works correctly but when I do 1.1 + 0.1 I get 1.2000000000000002 but all I want it to equal to is 1.2. When I do 1.0 + 0.1 I get 1.1 which is perfect but i don't get that for 1.1 + 0.1. So is there a way that I can get rid of the 000000000000002 from 1.2000000000000002?

我试着把十进制数加一个十进制数,它能正常工作,但当我做1.1 + 0。1时,得到1。2000000000000002,但我想让它等于1。2。当我做1。0 + 0。1时,我得到了1。1,这是完美的但我没有得到1。1 + 0。1。那么有没有一种方法可以让我从1。2000000000000002中去掉000000000000002?

Thanks.

谢谢。

3 个解决方案

#1


5  

As has been stated countless times, 0.1 cannot be represented exactly in IEEE 754 floating point. You can read all about why in What Every Computer Scientist Should Know About Floating-Point Arithmetic or The Floating Point Guide

如前所述,在IEEE 754浮点数中,0.1不能精确地表示。你可以读懂为什么每个计算机科学家都应该知道浮点运算或浮点指南。

You can trucate or round the value:

你可以用卡车或圆形物来运输:

>>> round(1.1+.1,2)
1.2
>>> "%.*f" % (1, 1.1+.1 )
'1.2'
>>> s=str(1.1+.1)
>>> s[0:s.find('.')+2]
'1.2'

If you want exact representation of those values, consider using the Decimal module:

如果您想要精确地表示这些值,请考虑使用十进制模块:

>>> import decimal
>>> decimal.Decimal('1.1')+decimal.Decimal('.1')
Decimal('1.2')

Note that you need to start with the string representation of your float, '0.1' since 0.1 is not exactly representable in binary in IEEE floating point:

注意,你需要从你的浮点数的字符串表示开始,0。0,因为0。0在IEEE浮点数中是不完全可以表示的:

>>> decimal.Decimal(.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')

To then get a string representation back after you calculate, you can use str:

然后在计算后得到一个字符串表示,可以使用str:

>>> str(sum(map(decimal.Decimal,['.1','.1','.5','.5'])))
'1.2'

Another alternative is to use a rational number library such as Fractions:

另一种选择是使用一个合理的数字库,例如分数:

>>> from fractions import Fraction as Fr
>>> Fr(11,10)+Fr(1,10)
Fraction(6, 5)

With that result, you will still need to round, truncate, or use an arbitrary precision arithmetic package to get an exact number (depending on the inputs...)

有了这个结果,您仍然需要旋转、截断或使用任意精确的算术包来获得确切的数字(取决于输入…)

#2


1  

You can try string formatting, documentation here.

您可以尝试字符串格式,这里的文档。

>>> "%0.2f" % float(1.1 + 0.1)
'1.20'

Or Even:

甚至:

>>> "%0.1f" % float(1.1 + 0.1)
'1.2'

As to why, it is explicitly described on PEP 327 here.

至于原因,在PEP 327上有明确的描述。

#3


0  

This is the literal answer to your question:

这是对你的问题的字面回答:

float(str(1.1 + 0.1)[0:3])

If you're interested in the "why" of the problem then refer to the links provided in the question comments.

如果您对问题的“为什么”感兴趣,请参考问题注释中提供的链接。

#1


5  

As has been stated countless times, 0.1 cannot be represented exactly in IEEE 754 floating point. You can read all about why in What Every Computer Scientist Should Know About Floating-Point Arithmetic or The Floating Point Guide

如前所述,在IEEE 754浮点数中,0.1不能精确地表示。你可以读懂为什么每个计算机科学家都应该知道浮点运算或浮点指南。

You can trucate or round the value:

你可以用卡车或圆形物来运输:

>>> round(1.1+.1,2)
1.2
>>> "%.*f" % (1, 1.1+.1 )
'1.2'
>>> s=str(1.1+.1)
>>> s[0:s.find('.')+2]
'1.2'

If you want exact representation of those values, consider using the Decimal module:

如果您想要精确地表示这些值,请考虑使用十进制模块:

>>> import decimal
>>> decimal.Decimal('1.1')+decimal.Decimal('.1')
Decimal('1.2')

Note that you need to start with the string representation of your float, '0.1' since 0.1 is not exactly representable in binary in IEEE floating point:

注意,你需要从你的浮点数的字符串表示开始,0。0,因为0。0在IEEE浮点数中是不完全可以表示的:

>>> decimal.Decimal(.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')

To then get a string representation back after you calculate, you can use str:

然后在计算后得到一个字符串表示,可以使用str:

>>> str(sum(map(decimal.Decimal,['.1','.1','.5','.5'])))
'1.2'

Another alternative is to use a rational number library such as Fractions:

另一种选择是使用一个合理的数字库,例如分数:

>>> from fractions import Fraction as Fr
>>> Fr(11,10)+Fr(1,10)
Fraction(6, 5)

With that result, you will still need to round, truncate, or use an arbitrary precision arithmetic package to get an exact number (depending on the inputs...)

有了这个结果,您仍然需要旋转、截断或使用任意精确的算术包来获得确切的数字(取决于输入…)

#2


1  

You can try string formatting, documentation here.

您可以尝试字符串格式,这里的文档。

>>> "%0.2f" % float(1.1 + 0.1)
'1.20'

Or Even:

甚至:

>>> "%0.1f" % float(1.1 + 0.1)
'1.2'

As to why, it is explicitly described on PEP 327 here.

至于原因,在PEP 327上有明确的描述。

#3


0  

This is the literal answer to your question:

这是对你的问题的字面回答:

float(str(1.1 + 0.1)[0:3])

If you're interested in the "why" of the problem then refer to the links provided in the question comments.

如果您对问题的“为什么”感兴趣,请参考问题注释中提供的链接。