Re: FastCode RoundToEx



Hi

Incomplete and buggy code was given as an example. This lead to confusion.

We have to compare

function RoundToEX_Ref(const AValue: Extended; const ADigit:
TRoundToEXRangeExtended): Extended;
var
LFactor: Extended;
CW8087 : Word;

begin
CW8087 := Get8087CW;
if (ADigit < Low(TRoundToEXRangeExtended)) or (ADigit >
High(TRoundToEXRangeExtended)) then
raise Exception.Create('ADigit out of range');
try
SetPrecisionMode(pmExtended);
SetRoundMode(rmNearest);
LFactor := IntPower(10, ADigit);
Result := Round(AValue / LFactor) * LFactor;
finally
Set8087CW(CW8087);
end;
end;

and

function RoundToEX_JFH_Pas_1(const aValue: Extended; const
NbrDecFractDigits: TRoundToEXRangeExtended): Extended;
var LFactor: Extended; CW8087 : Word;
begin
CW8087 := Get8087CW;
if (NbrDecFractDigits < Low(TRoundToEXRangeExtended)) or
(NbrDecFractDigits > High(TRoundToEXRangeExtended))
then raise Exception.Create('NbrDecFractDigits out of range');
try
Set8087CW($1332);
{ i.e. extended precision and bankers rounding on a binary basis }
LFactor := IntPower(10, -NbrDecFractDigits);
Result := Round(aValue*LFactor)/LFactor;
finally
Set8087CW(CW8087);
end;
end;

Best regards
Dennis Kjaer Christensen


.



Relevant Pages