将str转化为float以后相减,会存在一些误差怎么解决;
比如:a:='99.768';
b:='99.768';
d:='99.769';
c:=strtofloat(a)-strtofloat(b);后c的值为0.00100000000545
c:=strtofloat(d)-strtofloat(b);后c的值为6.80011602582908E-16
比如:a:='99.768';
b:='99.768';
d:='99.769';
c:=strtofloat(a)-strtofloat(b);后c的值为0.00100000000545
c:=strtofloat(d)-strtofloat(b);后c的值为6.80011602582908E-16
如果不能用以上方法,那只能根据实际情况处理结果。
原则上还是尽量避免用浮点数直接比较。
// one way
c:=strtofloat(a)-strtofloat(b);
if c<0.0001 then c:=0.0;
或者两者的差值“过滤”一下:
// the other way
pf : real; //precision factor
i integer;
pf:= 0.0001;
i;=trunc((strtofloat(d)-strtofloat(b))/pf)
c:=i*pf;
建议用一个单独的函数。
但是不要i;=trunc((strtofloat(d)/pf)-trunc(strtofloat(b)/pf),可能会超过maxint.
That's all.
That's all.