up previous next
convert rational number to a binary float
MantissaAndExponent2(X: INT|RAT|RINGELEM, 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*2^E
where
M
has
Prec
bits.
The value of
NumDigits
is simply
Prec
.
The value of
exponent
is
FloorLog2(X,2)
, plus 1 if the mantissa "overflows".
The value of
mantissa
is an integer
M
satisfying
2^(Prec-1) <= |M| < 2^Prec-1
/**/ MantissaAndExponent2(1/2,8); -- 1/2 = 128*2^(-8)
record[NumDigits := 8, exponent := -1, mantissa := 128]
/**/ MantissaAndExponent2(65535, 10); -- rounds up
record[NumDigits := 10, exponent := 16, mantissa := 512]
|