Important: The information in this document is obsolete and should not be used for new development.
Converting Between Binary and Decimal Numbers
MathLib provides two functions that let you manually convert between binary and decimal formats.
dec2num Converts a decimal number to a binary number. num2dec Converts a binary number to a decimal number. Conversions between binary floating-point numbers and decimal numbers use structures of type
decimal
. Thedecimal
structure is defined in the header filefp.h
as
struct decimal { char sgn; char unused; short exp; struct { unsigned char length; unsigned char text[SIGDIGLEN]; unsigned char unused; } sig; } decimal;The value represented is
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]
tosig.text[SIGDIGLEN - 1]
).(-1)
sgnsig10exp
For example, if
sgn
equals 1,exp
equals -3, andsig
equals "85" (string lengthsig.length
equals 2, not shown), then the number represented is -0.085.
Conversions from binary to decimal use a decimal format structure to specify how the number should look in decimal. The
- 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 wordsgn
are implementation dependent.decform
structure is defined in the header filefp.h
as
struct decform { char style; /* FLOATDECIMAL or FIXEDDECIMAL */ char unused; short digits; } decform;If the
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.style
field of thedecform
structure equals 0 (in C,f.style == FLOATDECIMAL
), the output is formatted in floating style, with thedigits
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 Component Description 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. dddd One to four exponent digits
If the
style
field of thedecform
structure equals 1 (in C,f.style == FIXEDDECIMAL
), the output is formatted in fixed style, with thedigits
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.