Problem with eval in python
仅有的幸福
仅有的幸福 2017-06-22 11:52:25
0
3
862

As shown in the figure, when Python2.7 uses eval to calculate floating point expressions,

eval('0.3-0.1==0.2')  # 输出为False
eval('%d - %d == %d'%(0.3, 0.1, 0.2))  # 输出为True
eval('%s - %s == %s'%(0.3, 0.1, 0.2))  # 输出为False
eval('%s - %s == %s'%('0.3', '0.1', '0.2'))  # 输出为False

This is very confusing. I can’t find the answer online. Please give me guidance!


renew:

I have another question,
a=0.3-0.1, print(a) outputs 0.19999999999999998; b=0.2,
But print('%s==%s'%(a,b) ) but the output is 0.2==0.2,
Why does a change from 0.19999999999999998 to 0.2 when formatting the string?

仅有的幸福
仅有的幸福

reply all(3)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!