rvelthuis / DelphiBigNumbers

BigInteger and BigDecimal for Delphi
http://rvelthuis.de/programs/bigintegers.html
BSD 2-Clause "Simplified" License
119 stars 62 forks source link

Range check error #10

Closed Safrad closed 5 years ago

Safrad commented 5 years ago

Happens in GetExponent functions only if range checking is on.

Solution: replace Result := E - CDoubleBias with Result := Integer(E) - Integer(CDoubleBias) in FloatUtils.pas

function GetExponent(const AValue: Single): Integer; overload; var M, E: UInt32; begin M := GetRawSignificand(AValue); E := GetRawExponent(AValue); if (0 < E) and (E < CSingleExponentMask) then Result := Integer(E) - Integer(CSingleBias) else if E = 0 then if M = 0 then // +/- Zero Result := 0 else // Denormal Result := 1 - CSingleBias else // NaN or +/-Infinity Result := 0; end;

function GetExponent(const AValue: Double): Integer; overload; var M: UInt64; E: UInt32; begin M := GetRawSignificand(AValue); E := GetRawExponent(AValue); if (0 < E) and (E < CDoubleExponentMask) then Result := Integer(E) - Integer(CDoubleBias) else if E = 0 then if M = 0 then // +/-Zero Result := 0 else // Denormal Result := 1 - CDoubleBias else // NaN or +/-Infinity Result := 0; end;

function GetExponent(const AValue: Extended): Integer; overload; var M: UInt64; E: UInt32; begin M := PUInt64(@AValue)^; E := GetRawExponent(AValue); if (0 < E) and (E < CExtendedExponentMask) then Result := Integer(E) - Integer(CExtendedBias) else if E = 0 then if M = 0 then // +/- Zero Result := 0 else // Denormal Result := 1 - CExtendedBias else // NaN or +/-Infinity Result := 0; end;

rvelthuis commented 5 years ago

Range checks should be off. I'll add a directive to the source.

rvelthuis commented 5 years ago

Solved by making the local variables E signed.