Python中小数的精度 [英] Precision of decimals in Python

查看:261
本文介绍了Python中小数的精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思不是精度,因为小数点后显示多少个数字.我的意思是精度,就像我试图在此象形文字函数中使用的小数一样,总是比其应有的精度低十分之一.我尝试使用多种不同的策略,包括导入十进制模块.这是我要使用的功能.

I don't mean precision as in how many numbers are displayed after the decimal. I mean precision as in the decimal I am trying to use in this pictograph function keeps coming up one tenth shy of what it should be. I have tried using multiple different strategies including importing the decimal module. Here is the function I am trying to use.

values = [('tens', 10), ('fives', 5), ('ones', 1), ('tenths', 0.1)]

def get_digits(num):
    num = int(num * 10)
    num = float(num) / 10

    output_dict = {}
    for place, value in values:
        output_dict[place] = int(num // value)
        num = num % value

    return output_dict

使用get_digits(277.9)产生{'tens': 27, 'ones': 2, 'tenths': 8, 'fives': 1}

我需要说{'tens': 27, 'ones': 2, 'tenths': 9, 'fives': 1}

在字典填充后的十分之一上加1来修正它是行不通的,因为并非每个小数都会不精确地出现.

Fixing it by adding 1 to the tenths after the dictionary is populated does not work, because not every decimal comes out imprecisely.

get_digits(277.6)返回{'fives': 1, 'tenths': 6, 'tens': 27, 'ones': 2}

我一直在论坛上寻找答案,但没有得到任何答案.任何帮助将不胜感激.

I've been scouring the forums for an answer, but nothing quite gets it. Any assistance would be greatly appreciated.

推荐答案

正如我试图在注释中解释的那样,问题在于您使用的是浮点数.

As I tried to explain in comments, the problem is that you're using floating point numbers.

有关浮点的更多信息,请参见 1

For more information on floating point, see 1, 2.

问题在于277.9实际上不是 存储为277.9,而是非常接近"的东西:

The issue is that 277.9 is not actually stored as 277.9, but rather something "very close":

print('%.40f' % a)
277.8999999999999772626324556767940521240234

解决方案不是使用一些任意启发式来判断数学是否略有偏离并尝试对其进行更正.我们 知道 的数学运算略有偏离-这就是浮点数给我们的礼物和诅咒.

The solution isn't to use some arbitrary heuristic to tell whether the math is slightly off and try to correct for it. We know the math is slightly off -- that's the gift and curse given to us by floating point.

真正的解决方案是使用定点数学运算,例如,使用Python的 decimal 模块.

The real solution is to use fixed point math, for example, with Python's decimal module.

编辑

Edit

from decimal import Decimal

values = [
    ('tens',   Decimal(10)),
    ('fives',  Decimal(5)),
    ('ones',   Decimal(1)),
    ('tenths', Decimal('0.1'))
]

def get_digits(num):
    output_dict = {}
    for place, value in values:
        output_dict[place] = int(num // value)  # Cast from Decimal to int
        num = num % value
    return output_dict

num = Decimal('277.9')
print(get_digits(num))
# {'tens': 27, 'ones': 2, 'tenths': 9, 'fives': 1}

num = Decimal('277.6')
print(get_digits(num))
#{'tens': 27, 'ones': 2, 'tenths': 6, 'fives': 1}

上面的代码与您的代码非常相似,但是使用Python的十进制模块.无需进行启发式检查.该代码可以正常工作,因为小数表示准确.

The above code is very similar to yours, but uses Python's decimal module. No heuristic check is needed. The code just works, because decimals are represented accurately.

这篇关于Python中小数的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆