var rat ,alot :Extended ;
len_c :integer ; alot :=606.795
len_c :=100
rat := alot * len_c + 0.5;
rat := Trunc(rat);
rat := rat / len_c;
执行到 rat := alot * len_c + 0.5; 这时 rat的值为60680
为什么执行过 rat := Trunc(rat); 这句后 rat的值就变为了60679了
len_c :integer ; alot :=606.795
len_c :=100
rat := alot * len_c + 0.5;
rat := Trunc(rat);
rat := rat / len_c;
执行到 rat := alot * len_c + 0.5; 这时 rat的值为60680
为什么执行过 rat := Trunc(rat); 这句后 rat的值就变为了60679了
len_c :integer ;
alot :=606.795;
len_c :=100;
rat := alot * len_c + 0.5;//这里rat = 606.795 * 100 + 0.5 刚好等于60680啊
rat := Trunc(rat);//这里Trunc后变为了60679应该和rat的变量类型为Extended有关,你改为Real试下。
rat := rat / len_c;
Extended为超长浮点型 可以接收比较长的数字吧
Real 是什么样的结构呀
http://www.cnblogs.com/del/archive/2007/12/04/982167.html
//这样执行的结果就是60680
procedure TForm1.btn1Click(Sender: TObject);
var rat :Real ;
alot :Extended ;
len_c :integer ;
begin
alot :=606.795 ;
len_c :=100 ;
rat := alot * len_c + 0.5;
rat := Trunc(rat);
edt1.Text := FloatToStr(rat) ;
end;
//这样执行的结果就是60679
procedure TForm1.btn1Click(Sender: TObject);
var rat :Real ;
alot :Real ;
len_c :integer ;
begin
alot :=606.795 ;
len_c :=100 ;
rat := alot * len_c + 0.5;
rat := Trunc(rat);
edt1.Text := FloatToStr(rat) ;
end;//这样执行的结果就是60679
var rat :Extended ;
alot :Real ;//这样执行的结果就是60680
var rat :Extended ;
alot :Extended ;
这是为什么啊 。
用real来表示606.795和用Extended来表示606.795在内存中的结果有可能是不一样的,比如
real的结果可能是606.794999999,而Extended则可能是606.795000000000000000000000000000001
累积的结果导致LZ的上述现象
另外,你可以试试用
606.785来测试一下你的程序
或许你找一下资料看一下小数,浮点数在计算机中是如何存储的能够加深一些了解
可以网上查一下“浮点数二进制表示”应当可以解释一二。