These functions are to help visualize numbers in a more comprehensible format.
MantissaAndExponent(N, SigFig)
splits the number N
into three parts:
the sign, the mantissa (with SigFig
digits), and the exponent
FloatStr(N, SigFig)
convert the number N
into a string of the form
mantissa times power-of-ten, with SigFig
digits in the mantissa.
See also the functions ILogBase
(in the doc for ZZ
and QQ
).
The functions MantissaAndExponent
do the real work. The only tricky
parts were deciding how to round in the case of a tie, and correct
behaviour when the mantissa "overflows". I finally decided to round away
from zero: it is easy to implement, and I wanted a solution which was
symmetric about zero, so that MantissaAndExponent
applied to N
and
to -N
would always give the same result except for sign.
Mantissa overflow occurs only when the last digit rounds away from zero, and all the digits were 9 before rounding. This case would never occur if I'd chosen simply to truncate when forming the mantissa.
Printing of a MantExp
structure is simple rather than elegant.
The conversion in MantissaAndExponent
is rather slow when the input
number is large.
In principle the call to ILogBase
could fail because of overflow;
but in that case ILogBase
itself should report the problem.
In principle a mantissa overflow could trigger an exponent overflow (i.e. if the exponent was already the largest possible long).
2011