Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: PowerPC Numerics / Part 2 - The PowerPC Numerics C Implementation
Chapter 9 - Conversion Functions / Converting Between Binary and Decimal Numbers


dec2num

You can use the dec2num 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 the decimal structure.
DESCRIPTION
The dec2num function converts a decimal number in a decimal structure to a double format floating-point number. Conversions from the decimal structure type handle any sig 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 the float type, dec2num converts to the double type, and dec2numl converts to the long double type. The other two versions of this function, dec2s and dec2l, convert to the short and long integer types, respectively.

IMPORTANT
When you create a decimal structure, you must set sig.length to the size of the string you place in sig.text. You cannot leave the length field undefined.
Before using this function, you can use the numeric formatter (str2dec, described on page 9-21) to convert a decimal string to a decimal structure suitable for input to the dec2num function.

EXCEPTIONS
When the sig string is longer than 36 characters, the result is undefined.

SPECIAL CASES
The following special cases apply:

  • If sig.text[0] is "0" (zero), the decimal structure is converted to zero. For example, a decimal structure with sig = "0913" is converted to zero.
  • If sig.text[0] is "N", the decimal structure is converted to a NaN. The succeeding characters of sig 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", the decimal 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 */

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996