最近在做一个项目,其中有一段判断一个 Extended
浮点数是否为整数的代码。我用如下方式实现:
function IsInt(F: Extended): Boolean;
begin
result := Trunc(F)-F = 0; //整数部分等于自身
end;
测试了许多样例都过了,唯独这个没过:
IsInt(4.000000002*1000000000); //False
调试时发现: Trunc(F)
居然等于 4000000001
!开始以为是精度的问题,找了许多资料也没能解决。后来将 Extended
换成了 Double
,就通过了。百思不得其解中。
OOPS!
A comment box should be right here...But it was gone due to network issues :-(If you want to leave comments, make sure you have access to disqus.com.