Since the emergence of IEEE Standard 754, most floating point arithmetic is binary. Such a system must support conversion between more familiar decimal numbers and the internal binary formats.
Programmable calculators and some hardware and software systems offer decimal floating point arithmetic for accounting and other business applications. Even these systems require careful attention to map arbitrary decimal input into the supported decimal formats.
Our focus here is the typical case of binary arithmetic.
Whether input directly by a user or written in a computer program or a data file, a decimal value is either zero or a value of the form
± d d ... d • d ... d d
or perhaps
± d d ... d • d ... d d E ± n n
where the d
are decimal digits,
and the E
introduces a factor
\( 10 ^{\pm nn} \).
If all of the d
are zero,
the value converts to zero,
with the algebraic sign passing
through to the binary result, if the format supports
a sign on zero.
Otherwise, the value
can be rearranged to the mathematically equivalent form
± d • d d ... d * 10 ** h
where the leading d
is nonzero.
This decimal number can be converted to
the unique binary value
+---------------------+-------+ ± | 1 • b b b b b b b b | G R S | * 2**k +---------------------+-------+
where the sticky bit S
is the logical
OR
of all bits to the right of R
.
With the intermediate binary result in this form, the rest of the conversion proceeds as with the arithmetic operations.
A value of zero, \( \pm\infty \), or NaN requires no arithmetic. Otherwise, target decimal formats come in two flavors:
dd.dddd
d.ddddddEnn
Conversion to floating form is the more interesting case, analogous to the binary arithmetic itself. The input value may be expressed in normalized form as
+---------------------+ ± | 1 • b b b b b b b b | * 2**k +---------------------+
whether or not the value was denormalized
in the encoding. In this intermediate form, there is
no limit on the range of k
.
This value can be converted to the unique decimal intermediate form
+---------------------+-----+ ± | d • d d d . . . d d | G S | * 10**h +---------------------+-----+
where the d
s are decimal digits,
and the leading d
is nonzero.
G
is a decimal guard digit
and S
is a sticky bit, which
is nonzero only if some digit beyond G
is nonzero, in the exact representation.
This is analogous to binary projection.
There is no need for a Round digit
because the value is normalized and will never require
a one-digit left shift.
Projection onto decimal is simpler than projection onto binary, because typically exponent overflow and underflow are not an issue on output. The only challenge is rounding.
Using the language of Projection here is what we mean by rounding a decimal result. Given a value
+---------------------+-----+ ± | d • d d d . . . d d | G S | * 10**h +---------------------+-----+
regardless of sign, we
round up (in magnitude)
by adding \( 1 \)
into the lowest-order d
.
If every d
is
9
, then there is a carry out
of the leading 9
, so the
significant digits must be right-shifted
one place and the exponent h
incremented.
We round down by taking no action on the significant digits.
In either case,
we discard Guard digit
G
and Sticky bit S
after rounding.
G
or S
is nonzero, then round up; otherwise, round down
G
or S
is nonzero, then round up; otherwise round down
G > 5
,
or if G = 5
and either
(a) S
is 1
or (b) the lowest-order d
is odd;
otherwise, round down
As with binary, if
G
and S
are both zero, the result needs no rounding.
Otherwise,
the intermediate
value lies between representable values in
the desired decimal form.
Round to Nearest chooses the nearer of the two adjacent decimal values. It's helpful to list the cases:
± | d • d d d . . . d d | 0-4 S round down ± | d • d d d . . . d d | 6-9 S round up ± | d • d d d . . . d d | 5 1 round up ± | d • d d d . . . d even | 5 0 round down ± | d • d d d . . . d odd | 5 0 round up
The last two cases illustrate the decimal version of unbiased round-to-nearest.
Radix conversion is a rich topic. This booklet presents the basic facts about the operations.
In pages yet to appear, we will look at how many decimal digits it takes to distinguish binary floating point values and we'll consider different ways to think of best possible conversions.