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

MathLib provides two functions that let you manually convert between binary and decimal formats.
dec2numConverts a decimal number to a binary number.
num2decConverts a binary number to a decimal number.

Conversions between binary floating-point numbers and decimal numbers use structures of type decimal. The decimal structure is defined in the header file fp.h as

struct decimal 
{
   char sgn;
   char unused;
   short exp;
   struct 
   {
      unsigned char length;
      unsigned char text[SIGDIGLEN];
      unsigned char unused;
   } sig;
} decimal;
sgn
The sign of the number (0 is positive, 1 is negative).
exp
The exponent of the number. The exponent is expressed as a power of 10.
sig
The significand. String sig.text contains the significand as a decimal integer in the form of a string, that is, with the string length in the zeroth byte (sig.length) and the initial character of the string in the first byte (sig.text[0] to sig.text[SIGDIGLEN - 1]).
The value represented is

(-1)sgnsig10exp

For example, if sgn equals 1, exp equals -3, and sig equals "85" (string length sig.length equals 2, not shown), then the number represented is -0.085.

Note
The maximum length of the string sig is implementation dependent. The limit is 36 characters. Also, the representations of 0 and 1 in the 16-bit word sgn are implementation dependent.
Conversions from binary to decimal use a decimal format structure to specify how the number should look in decimal. The decform structure is defined in the header file fp.h as

struct decform 
{
   char style; /* FLOATDECIMAL or FIXEDDECIMAL */
   char unused;
   short digits;
} decform;
style
The style of output. This field equals 0 (FLOATDECIMAL) for floating and 1 (FIXEDDECIMAL) for fixed.
digits
The number of significant digits for the floating style and the number of digits to the right of the decimal point for the fixed style. (The value of digits may be negative if the style is fixed.)
Note
Formatting details, such as the representations of 0 and 1 in the 16-bit style word, are implementation dependent.
If the style field of the decform structure equals 0 (in C, f.style == FLOATDECIMAL), the output is formatted in floating style, with the digits field specifying the number of significant digits required. Output in floating style is represented in the following format; Table 9-8 defines its components.

[- |    ]m[.nnn]e[+ | -]dddd
Table 9-8 Format of decimal output string in floating style
ComponentDescription
Minus sign (-) or space Minus sign if sgn = 1; space if sgn = 0
m Single digit, 0 only if value represented is 0
Point (.) Present if digits > 1
nnn String of digits; present if digits > 1
e The letter e
Plus sign (+) or minus sign (-)Plus sign if exp 0; minus sign if exp 0.
ddddOne to four exponent digits

If the style field of the decform structure equals 1 (in C, f.style == FIXEDDECIMAL), the output is formatted in fixed style, with the digits field specifying the number of digits to follow the decimal point. All output in fixed style is represented in the following format; Table 9-9 defines its components.

[-]mmm[.nnn]

Table 9-9 Format of decimal output string in fixed style
Component Description
Minus sign (\xC7) Present if sgn = 1
mmm String of digits; at least one digit but no superfluous leading zeros
Point (.) Present if digits > 0
nnn String of digits of length equal to digits; present if digits > 0

Note that if sgn equals 0, then floating-style output begins with a space but fixed-style output does not.

Double-double values being converted to decimal strings are first rounded to 113 bits (if they in fact span more than that number of bits in their significands) and then converted to the decimal string of the desired length.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996