up previous next
MantissaAndExponent2 --
convert rational number to a binary float
|
MantissaAndExponent2(X: INT|RAT, Prec: INT): RECORD
MantissaAndExponent2(X: RINGELEM): RECORD |
The first form of this function converts an integer or 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
The second form of this function applies to elements of a "twin-float"
ring. In this case the "precision" is determined directly from the
twin-float value; since twin-float arithmetic is based on a randomized
heuristic, repeating a computation may give a slightly different result
(and this can be seen in the output of
MantissaAndExponent2.
/**/ 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]
|