Fraction Coding
Fraction Coding
Although COFF tools recognize values in integer, hex, binary, and other forms, they understand
only integer, or non-fractional values. To use fractions within the C28x, it is necessary to describe
them as though they were integers. This turns out to be a very simple trick. Consider the
following number lines:
How is a fraction coded?
How is a fraction coded?
~ 1
~ 1
0
0
–
–
½
½
–
–
1
1
½
½
Fractions
Fractions
~ 32K
~ 32K
0
0
–
–
16K
16K
–
–
32K
32K
16K
16K
Integers
Integers
7FFF
7FFF
0000
0000
C000
C000
8000
8000
4000
4000
Hex
Hex
void main(void) {
void main(void) {
int
int
coef
coef
= 32768 * 707 / 1000;
= 32768 * 707 / 1000;
}
}
⇒
⇒
*32768
*32768
Example: represent the fraction number 0.707
Example: represent the fraction number 0.707
By multiplying a fraction by 32K (32768), a normalized fraction is created, which can be passed
through the COFF tools as an integer. Once in the C28x, the normalized fraction looks and
behaves exactly as a fraction. Thus, when using fractional constants in a C28x program, the coder
first multiplies the fraction by 32768, and uses the resulting integer (rounded to the nearest whole
value) to represent the fraction.
The following is a simple, but effective method for getting fractions past the assembler:
1. Express the fraction as a decimal number (drop the decimal point).
2. Multiply by 32768.
3. Divide by the proper multiple of 10 to restore the decimal position.
Examples:
• To represent 0.62: 32768 x 62 / 100
• To represent 0.1405: 32768 x 1405 / 10000
This method produces a valid number accurate to 16 bits. You will not need to do the math
yourself, and changing values in your code becomes rather simple.
C28x - Numerical Concepts & IQmath 8 - 11