且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

可以将以.0结尾的浮点数视为数学整数吗?

更新时间:2023-02-19 23:11:34

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