up previous next
MantissaAndExponent10 --
convert rational number to a float
|
MantissaAndExponent10(X: INT|RAT, Prec: INT): RECORD |
This function converts a rational number into a
RECORD with components
named
exponent,
mantissa and
NumDigits.
If
X=0, all fields of the record are set to zero.
For non-zero
X the fields give the best representation of the form
M*10^E
where
M has
Prec decimal digits.
The value of
NumDigits is simply
Prec.
The value of
exponent is
FloorLog10(X), plus 1 if the mantissa "overflows".
The value of
mantissa is an integer
M satisfying
10^(Prec-1) <= |M| < 10^Prec-1
/**/ MantissaAndExponent10(1/2,3); -- 1/2 = 5.00*10^(-1)
record[NumDigits := 3, exponent := -1, mantissa := 500]
/**/ MantissaAndExponent10(0.99999, 4); -- 0.99999 rounds up to give 1.000
record[NumDigits := 4, exponent := 0, mantissa := 1000]
|