procedure TForm1.Button1Click(Sender: TObject);
var
    tempInt: integer;
begin
    tempInt := Floor((7.2 - 7) * 10);
    showmessage(IntToStr(tempInt));
end;//run the procedure behind and tell me why?
//why the result is 1 not 2????
//Is it delphi`s bug?

解决方案 »

  1.   

    搞不清楚,tempInt := Floor((7.2 - 7) * 100);
    是会等于19,那么也就是说系统(7.2-7)*10=1.9
    floor(1.9)=1,var
        tempInt: double;
        i: Integer;
    begin
        tempInt := (7.2 - 7)* 100;
        i := floor(tempInt);
        showmessage(IntToStr(i));
    end;这样就可以了!呵呵
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
        tempInt: integer;
    begin
        tempInt := Floor((7.2 - 7.0) * 10);
        showmessage(IntToStr(tempInt));
    end;这样可以,不知什么原因
      

  3.   

    不是BUG,是你对浮点数字理解不够。
     Floor(1.99999999999)你觉得应该是多少?(7.2-7)*10 = 1.999999999999999999999999999999999999...
    (7.2-7)*10 != 2
      

  4.   

    7.2-7)*10 = 1.999999999999999999999999999999999999...
    but why????
      

  5.   

    因为浮点数是不精确的数,只能和数很接近,但不一定等于该数
    你可以找找资料看看浮点数的内存存储方式
    其实,浮点数字随着阶码长度和尾数长度不同,其有意义的位数也是不同的.
    比如我写的1.999999999999999999999,如果是single类型,有效数字只有8位
    也就是 1.9999999469584和1.999999904343543在内存中的表示是无区别的,
    换个角度说19999999324.0和119999999444也是没有区别的.
      

  6.   

    再给例子,你可以看看结果
    procedure TForm1.Button1Click(Sender: TObject);
    var
      a, b: single;//double
    begin
      a := 0.5000000001;
      b := 0.5;
      if a = b then
        ShowMessage('ok');end;类型分别换成single和double是有不同结果的,如果你不明白浮点数的话,会认为在single的情况下,delphi又出bug了.
      

  7.   

    在single的情况下
    无论 a:=0.500000003241342141234....后面怎么写
    a = b都是成立的
    因为,在内存中他们是一样的
      

  8.   

    听到了新消息:李维去CodeGear了,呵呵!
      

  9.   

    http://stephensuen.spaces.live.com/Blog/cns!1p1G_DGhjYiYGmj6keNZQAcw!172.entry
    看了上面的文章,发现对于对于float认识太浅薄了,揭帖,给分,留解决方案。
      

  10.   

    有位高人指点道:
        floor = round(x - 0.5);
        ceil = round(x + 0.5);