具体图片如下:http://computer.mblogger.cn/sanoul/posts/50874.aspx具体问题如下:  两个变量(D和AInvoiceAmount)都是Double型,在程序运行过程中赋值来赋值去,在做一个If判断的时候出现了一个问题,无论D和AInvoiceAmount这两个变量怎么相等,但运行的结果总是D>AInvoiceAmount,小弟百思不得其解,请求各位帮助。

解决方案 »

  1.   

    没人解答么?我已经用Round函数来比较大小了,但是为什么会有这种情况呢?
      

  2.   

    浮点数误差而已,一般都使用一个系数进行校对而已,请参阅Math.pas 的 SameValue 和 IsZero 函数。
    1226行:const
      FuzzFactor = 1000;
      ExtendedResolution = 1E-19 * FuzzFactor;
      DoubleResolution   = 1E-15 * FuzzFactor;
      SingleResolution   = 1E-7 * FuzzFactor;function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
    begin
      if Epsilon = 0 then
        Epsilon := Max(Min(Abs(A), Abs(B)) * ExtendedResolution, ExtendedResolution);
      if A > B then
        Result := (A - B) <= Epsilon
      else
        Result := (B - A) <= Epsilon;
    end;function SameValue(const A, B: Double; Epsilon: Double): Boolean;
    begin
      if Epsilon = 0 then
        Epsilon := Max(Min(Abs(A), Abs(B)) * DoubleResolution, DoubleResolution);
      if A > B then
        Result := (A - B) <= Epsilon
      else
        Result := (B - A) <= Epsilon;
    end;function SameValue(const A, B: Single; Epsilon: Single): Boolean;
    begin
      if Epsilon = 0 then
        Epsilon := Max(Min(Abs(A), Abs(B)) * SingleResolution, SingleResolution);
      if A > B then
        Result := (A - B) <= Epsilon
      else
        Result := (B - A) <= Epsilon;
    end;function IsZero(const A: Extended; Epsilon: Extended): Boolean;
    begin
      if Epsilon = 0 then
        Epsilon := ExtendedResolution;
      Result := Abs(A) <= Epsilon;
    end;function IsZero(const A: Double; Epsilon: Double): Boolean;
    begin
      if Epsilon = 0 then
        Epsilon := DoubleResolution;
      Result := Abs(A) <= Epsilon;
    end;function IsZero(const A: Single; Epsilon: Single): Boolean;
    begin
      if Epsilon = 0 then
        Epsilon := SingleResolution;
      Result := Abs(A) <= Epsilon;
    end;=============================================
    SameValue function
    Indicates whether two floating-point values are (approximately) equal.UnitMathCategoryArithmetic routinesDelphi syntax:function SameValue(const A, B: Single; Epsilon: Single = 0): Boolean; overload;
    function SameValue(const A, B: Double; Epsilon: Double = 0): Boolean; overload;
    function SameValue(const A, B: Extended; Epsilon: Extended = 0): Boolean; overload;C++ syntax:extern PACKAGE bool __fastcall SameValue(const float A, const float B, float Epsilon = 0);
    extern PACKAGE bool __fastcall SameValue(const double A, const double B, double Epsilon = 0);
    extern PACKAGE bool __fastcall SameValue(const Extended A, const Extended B, Extended Epsilon = 0);DescriptionCall SameValue to determine when two floating-point values are equal, or approximately equal.A and B are the values to compare.Epsilon is the maximum amount by which A and B can differ and still be considered the same value.