Python float相等怪异 [英] Python float equality weirdness

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

问题描述

今晚用Python看到了一些意外的行为。为什么以下打印输出不相等?!

Seeing some unexpected behavior with Python tonight. Why is the following printing out 'not equal'?!

num = 1.00
num -= .95
nickel = .05

if nickel != num:
    print 'not equal'
else:
    print 'equal' 


推荐答案

每个计算机科学家应了解的浮点算术知识

>>> num = 1.00
>>> num
1.0
>>> num -= 0.95
>>> num
0.050000000000000044
>>> nickel = .05
>>> nickel
0.05

这篇关于Python float相等怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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