浮点数误差而已,一般都使用一个系数进行校对而已,请参阅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.
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.