Important: The information in this document is obsolete and should not be used for new development.
dec2num
You can use thedec2numfunction 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
decimalstructure to be converted. See page 9-13 for the definition of thedecimalstructure.DESCRIPTION
Thedec2numfunction converts a decimal number in adecimalstructure to a double format floating-point number. Conversions from thedecimalstructure type handle anysigstring 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:
dec2fconverts the decimal number to thefloattype,dec2numconverts to thedoubletype, anddec2numlconverts to thelong doubletype. The other two versions of this function,dec2sanddec2l, convert to the short and long integer types, respectively.
Before using this function, you can use the numeric formatter (
- IMPORTANT
- When you create a
decimalstructure, you must setsig.lengthto the size of the string you place insig.text. You cannot leave thelengthfield undefined.![]()
str2dec, described on page 9-21) to convert a decimal string to a decimal structure suitable for input to thedec2numfunction.EXCEPTIONS
When thesigstring is longer than 36 characters, the result is undefined.SPECIAL CASES
The following special cases apply:
- If
sig.text[0]is "0" (zero), thedecimalstructure is converted to zero. For example, adecimalstructure withsig= "0913" is converted to zero.- If
sig.text[0]is "N", thedecimalstructure is converted to a NaN. The succeeding characters ofsigare 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", thedecimalstructure 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 */