Unit FltMath

Classes

TBetterFloatProperty - New floating point property editor that handles NaN and Infinity correctly

Functions

ClearExceptions - Clear any pending exception bits in the status word
FloatToStr - Like SysUtils functions, but prints Nan and Infinity correctly.
FloatToStrF - Like SysUtils functions, but prints Nan and Infinity correctly.
FloatToText -
FltEQ - We are comparing two finite values, so take the difference and compare that against the scaled Epsilon.
FltGE -
FltGT - We are comparing two finite values, so take the difference and compare that against the scaled Epsilon.
FltLE - Approximate comparison

We are comparing two finite values, so take the difference and compare that against the scaled Epsilon.
FltLT - Approximate comparison, from Knuth, Seminumerical Algorithms, 2nd ed.
FltNE -
GetControlWord - Return the FPU control word
GetExceptionMask - Return the exception mask from the control word.
GetPrecisionMode - Return the current precision control mode
GetRoundMode - Return the current rounding mode
IsInfinity - Return whether Value is an Infinity (positive or negative).
IsNan - Return whether Value is Not-a-Number.
Ln2 - Log10(2)
Log10 - LogE(2) } { Log, base 10 of X

Compute a common (base 10) logarithm.
Log2 - Pi
Log210 - Return some useful constants.
Log2E - Log2(10)
Pi - Log2(E)
Register - Register the new property editor for all floating point types
SetControlWord - Set the FPU control word
SetExceptionMask - Set a new exception mask and return the old one
SetPrecisionMode -
SetRoundMode - Set the rounding mode and return the old mode
StrToFloat - Like SysUtils.

Types

TExceptionMask
TFpuException
TPrecisionMode
TRoundMode

Constants

Epsilon
Infinity
Nan
sInf
sNan

Variables


Functions


procedure ClearExceptions;

Clear any pending exception bits in the status word

function FloatToStr(Value: Extended): string;

Like SysUtils functions, but prints Nan and Infinity correctly.

function FloatToStrF(Value: Extended; Format: TFLoatFormat; Precision, Digits: Integer): string;

Like SysUtils functions, but prints Nan and Infinity correctly.

function FloatToText(Buffer: PChar; Value: Extended; Format: TFloatFormat; Precision, Digits: Integer): Integer;


function FltEQ(A, B: Extended): Boolean;

We are comparing two finite values, so take the difference and compare that against the scaled Epsilon.

function FltGE(A, B: Extended): Boolean;


function FltGT(A, B: Extended): Boolean;

We are comparing two finite values, so take the difference and compare that against the scaled Epsilon.

function FltLE(A, B: Extended): Boolean;

Approximate comparison

We are comparing two finite values, so take the difference and compare that against the scaled Epsilon.


function FltLT(A, B: Extended): Boolean;

Approximate comparison, from Knuth, Seminumerical Algorithms, 2nd ed., Addison-Wesley, 1981, pp. 217-20.

function FltNE(A, B: Extended): Boolean;


function GetControlWord: Word;

Return the FPU control word

function GetExceptionMask: TExceptionMask;

Return the exception mask from the control word. Any element set in the mask prevents the FPU from raising that kind of exception. Instead, it returns its best attempt at a value, often Nan or an infinity. The value depends on the operation and the current rounding mode.

function GetPrecisionMode: TPrecisionMode;

Return the current precision control mode

function GetRoundMode: TRoundMode;

Return the current rounding mode

function IsInfinity(Value: Extended): Boolean;

Return whether Value is an Infinity (positive or negative).

function IsNan(Value: Extended): Boolean;

Return whether Value is Not-a-Number. Note that signalling NaNs do not raise an exception in this case, but just return True.

function Ln2: Extended;

Log10(2)

function Log10(X: Extended): Extended;

LogE(2) } { Log, base 10 of X

Compute a common (base 10) logarithm. If X is near 1.0, then we use the FYL2XP1 instruction instead of FYL2X. "Near" means between 1.0 and 1+Sqrt(2)/2. We use an approximation for Sqrt(2)/2, so we don't have to compute it. The exact value isn't important, since FYL2X works fine for values near the transition.


function Log2: Extended;

Pi

function Log210: Extended;

Return some useful constants. The inline instructions are very fast if there is a coprocessor.

function Log2E: Extended;

Log2(10)

function Pi: Extended;

Log2(E)

procedure Register;

Register the new property editor for all floating point types

procedure SetControlWord(Ctl: Word);

Set the FPU control word

function SetExceptionMask(Mask: TExceptionMask): TExceptionMask;

Set a new exception mask and return the old one

function SetPrecisionMode(Precision: TPrecisionMode): TPrecisionMode;


function SetRoundMode(RoundMode: TRoundMode): TRoundMode;

Set the rounding mode and return the old mode

function StrToFloat(const Str: string): Extended;

Like SysUtils.StrToFloat, but recognizes Nan and Infinity.

Convert a string to a floating point value, correctly recognizing Nan and Infinity.


Types


TExceptionMask = set of TFpuException

TFpuException = (exInvalidOp, exDenormalized, exZeroDivide,
                     exOverflow, exUnderflow, exPrecision);

TPrecisionMode = (pmSingle, pmReserved, pmDouble, pmExtended);

TRoundMode = (rmNearest, rmDown, rnUp, rmTruncate);
string resource for 'Infinity'

Constants

Epsilon = 1e-17

Infinity = 0.0

Nan = 0.0

These are difficult to set as constants, so set them when the program starts

sInf = 4241

string resource for 'NaN'

sNan = 4240

epsilon for approximate comparisons

Variables