Important: The information in this document is obsolete and should not be used for new development.
num2dec
You can use thenum2dec
function to convert a binary floating-point number to a decimal number.
void num2dec (const decform *f, double_t x, decimal *d); void num2decl (const decform *f, long double x, decimal *d);
f
- A
decform
structure that describes how the number should look in decimal. See page 9-14 for a description of thedecform
structure.x
- The floating-point number to be converted.
d
- Upon return, a pointer to the
decimal
structure containing the number. See page 9-13 for a description of thedecimal
structure.DESCRIPTION
Thenum2dec
function converts a floating-point number to a decimal number. The decimal number is contained in adecimal
structure. Each conversion to adecimal
structured
is controlled by adecform
structuref
. All implementations allow 36 digits to be returned in thesig
field of thedecimal
structure. The implied decimal point is at the right end ofsig
, withexp
set accordingly.After using the
num2dec
function, you can use thedec2str
function to convert thedecimal
structure to a character string.
- IMPORTANT
- Use the same decimal format structure settings for
dec2str
as you used fornum2dec
; otherwise, the results are unspecified.EXCEPTIONS
When the number of digits specified in adecform
structure exceeds an implementation maximum (which is 36), the result is undefined.A number might be too large to represent in a chosen fixed style. For instance, if the implementation's maximum length for
sig
is 36, then (which requires 33 digits to the left of the point in fixed-style representations) is too large for a fixed-style representation specifying more than two digits to the right of the point. If a number is too large for a chosen fixed style, then (depending on the numeric implementation) one of two results is returned: an implementation might return the most significant digits of the number insig
and setexp
so that thedecimal
structure contains a valid floating-style approximation of the number; alternatively, an implementation might simply setsig
to the string "?". Note that in any implementation, the following test determines whether a nonzero finite number is too large for the chosen fixed style.
decimal d; decform f; int too_big; /* Boolean */ too_big = (-d.exp != f.digits) || (d.sig.text[0] == "?");For fixed-point formatting, PowerPC Numerics treats a negative value fordigits
as a specification for rounding to the left of the decimal point; for example,digits
= -2 means to round to hundreds. For floating-point formatting, a negative value fordigits
gives unspecified results.SPECIAL CASES
In all three of these cases,
- For zeros, the character "0" is placed in
sig.text[0]
.- For NaNs, The character "N" is placed in
sig.text[0]
. The character "N" might be followed by a hexadecimal representation of the input significand. The third and fourth hexadecimal digits following the "N" give the NaN code. For example, "N4021000000000000" has NaN code 0x21.- For Infinities, the character "I" is placed in
sig.text[0]
.
exp
is undefined.EXAMPLES
decimal d; decform f; double_t fp_num = 1.000007; f.style = FLOATDECIMAL; /* floating-point format */ f.digits = 7; /* seven significant digits */ num2dec(&f, fp_num, &d); /* d now contains 1.000007 expressed in decimal structure */