可以将以.0结尾的浮点数视为数学整数吗? [英] Can floating point numbers ending with `.0` be considered as mathematical integers

查看:61
本文介绍了可以将以.0结尾的浮点数视为数学整数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道使用浮点数会导致奇怪的结果:

We all know that using floating point numbers can lead to strange results:

>>> 0.1*3
0.30000000000000004

我的问题:是否可以将所有以 .0 结尾的浮点数视为数学整数?

My question: can all floating point numbers ending with .0 be considered as mathematical integers?

我的用例:

>>> x = 3.4296875
>>> 2*x*(128*x-695)+1856
100.0

是否可以断言没有古怪之处,而得出的结果是真实且精确的100?我的意思是不用手动进行计算!

Is it possible to assert that there is no quirk and that the result is a true and precise 100? I mean without doing the calculation by hand!

谢谢!

推荐答案

浮点算术不是怪异的魔术.这是一种精确的算术运算(毕竟,这是一台处理它的机器,而不是陶醉的妖精).它比自然数或实数算法复杂得多,或者甚至未知得多.如此短暂或漫长,答案是否定的.甚至对于类型而言有时看起来也很酷的Python也意识到这一点:

Floating point arithmetic is not a quirk magical mess. It is a precise arithmetic (after all, it is a machine which deals with it, not an intoxicated leprechaun). It is just much more complicated or rather much more unknown, than natural numbers or real numbers arithmetic. So short or long, the answer is no. Even Python, which looks sometimes (too) cool concerning types, is aware of this:

>>> x = 1+1e-100
>>> x == 1
True
>>> isinstance(x,int)
False
>>> isinstance(x,float)
True

威廉·卡汉(William Kahan)的著作具有足够的教学意义,使我们意识到诱骗短途旅行的所有危险.

The work of William Kahan has been pedagogical enough to make us being aware of all the dangers of tempting short paths.

这篇关于可以将以.0结尾的浮点数视为数学整数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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