Important: The information in this document is obsolete and should not be used for new development.
dec2num
You can use thedec2num
function to convert a decimal number to a binary floating-point number.
float dec2f (const decimal *d); double_t dec2num (const decimal *d); long double dec2numl (const decimal *d); short int dec2s (const decimal *d); long int dec2l (const decimal *d);
d
- The
decimal
structure to be converted. See page 9-13 for the definition of thedecimal
structure.DESCRIPTION
Thedec2num
function converts a decimal number in adecimal
structure to a double format floating-point number. Conversions from thedecimal
structure type handle anysig
string of length 36 or less (with an implicit decimal point at the right end).There are three versions of this function that convert to a floating-point type:
dec2f
converts the decimal number to thefloat
type,dec2num
converts to thedouble
type, anddec2numl
converts to thelong double
type. The other two versions of this function,dec2s
anddec2l
, convert to the short and long integer types, respectively.
Before using this function, you can use the numeric formatter (
- IMPORTANT
- When you create a
decimal
structure, you must setsig.length
to the size of the string you place insig.text
. You cannot leave thelength
field undefined.str2dec
, described on page 9-21) to convert a decimal string to a decimal structure suitable for input to thedec2num
function.EXCEPTIONS
When thesig
string is longer than 36 characters, the result is undefined.SPECIAL CASES
The following special cases apply:
- If
sig.text[0]
is "0" (zero), thedecimal
structure is converted to zero. For example, adecimal
structure withsig
= "0913" is converted to zero.- If
sig.text[0]
is "N", thedecimal
structure is converted to a NaN. The succeeding characters ofsig
are interpreted as a hexadecimal representation of the result's significand: if fewer than four characters follow the N, then they are right aligned in the high-order 15 bits of the field f illustrated in the section "Formats" in Chapter 2, "Floating-Point Data Formats"; if four or more characters follow the N, then they are left aligned in the result's significand.- If
sig.text[0]
is "I", thedecimal
structure is converted to an Infinity.
EXAMPLES
decimal d; double_t result; d.sgn = 0; d.exp = 3; d.sig.length = 3; d.sig.text[0] = '2'; d.sig.text[1] = '0'; d.sig.text[2] = '8'; result = dec2num(&d); /* result = 208,000 stored in double format */