Trunc()函数 [英] Trunc() function

查看:253
本文介绍了Trunc()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Trunc函数的结果是不同的?

  procedure TForm1.Button1Click(Sender:TObject) ; 
var
D:Double;
E:延伸;
I:Int64;
begin
D:= Frac(101/100)* 100;
E:= Frac(101/100)* 100;
I:= Trunc(D);
ShowMessage('Trunc(Double):'+ IntToStr(I)); // Trunc(Double):1
I:= Trunc(E);
ShowMessage('Trunc(Extended):'+ IntToStr(I)); // Trunc(扩展):0
end;


解决方案

格式化函数并不总是显示实际的数字
真正的数字和精度可能会非常棘手。



检查这个代码,我使用更精确的我想在屏幕上看到:

  D:= Frac(101/100); 
E:= Frac(101/100);
ShowMessage(FloatToStrF(D,ffFixed,15,20));
ShowMessage(FloatToStrF(E,ffFixed,18,20));

似乎 D code> 0.010000000000 而 E 就像 0.00999999999


$ b Edit 扩展类型比双精度类型。
如果我们试图用FloatToString()显示D和E的值,那么即使实际值不相同,也可能得到相同的结果。 b $ b

look the follow code, why the result of Trunc function is different?

procedure TForm1.Button1Click(Sender: TObject);
var
  D: Double;
  E: Extended;
  I: Int64;
begin
  D := Frac(101 / 100) * 100;
  E := Frac(101 / 100) * 100;
  I := Trunc(D);
  ShowMessage('Trunc(Double): ' + IntToStr(I));  // Trunc(Double): 1
  I := Trunc(E);
  ShowMessage('Trunc(Extended): ' + IntToStr(I)); // Trunc(Extended): 0
end;

解决方案

Formatting functions don't always display the actual numbers (data).
Real numbers and precision can be tricky.

Check out this code where I use more precision on what I want to see on the screen:

  D := Frac(101 / 100);
  E := Frac(101 / 100);
  ShowMessage(FloatToStrF(D, ffFixed, 15, 20));
  ShowMessage(FloatToStrF(E, ffFixed, 18, 20));

It appears that D is something like 0.010000000000 while E is like 0.00999999999.

Edit: Extended type has better precision than Double type. If we try to display the values of D and E with FloatToString() we'll probably get the same result, even though the actual values are not the same.

这篇关于Trunc()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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