ahausladen / JsonDataObjects

JSON parser for Delphi 2009 and newer
MIT License
413 stars 160 forks source link

CompareFloatRelative #29

Closed adem0x closed 8 years ago

adem0x commented 8 years ago

In TestJsonDataObjects.pas, in procedure TestTJsonBaseObject.TestVariant, there's a call to CompareFloatRelative().

I have searched the Net and all my code repo (of about 20 years) and I couldn't find it.

It turns out it exists in XE10 (and maybe in other versions above XE2, I didn't check) and since it is MPL licensed, I am copying the relevant code below --in case anyone else needs it.

BTW, these are overloaded.

function CompareFloatRelative(expected, actual: Extended) : Boolean;
begin
  Result := CompareFloatRelative(expected, actual, 0.0000001); //This is a 99.99999% accuracy
end;

function CompareFloatRelative(expected, actual: Extended; RelativeError : Extended) : Boolean;
begin
  Result := False;
  if expected = actual then  := True
  else if abs((actual - expected) / expected) < RelativeError then Result := True;
end;