0
\$\begingroup\$

I have been trying to implement an assembly routine in MSP430 for division. I already got the code for the division from the Horner Division Algorithm. The problem is that I only got the integer part. How I can get the fractional part from a division? For example: 5/2 = 2.5. How do I get the 0.5 part?

\$\endgroup\$
  • \$\begingroup\$ The MSP430 does not have a floating-point data type. How would the fractional part be represented? \$\endgroup\$ – CL. Nov 12 '15 at 19:19
1
\$\begingroup\$

You should get the remainder.

To turn that into a fraction you first need to decide between fixed and floating point; and if fixed, how many fractional bits.

Fixed point is easy : if you decide you want 8 fractional bits, just divide 2^8 * remainder / denominator, and use the size of that operation's remainder to determine rounding.

In your example, that would give

(256 * 1) / 2 = 128 as your fractional part, i.e. 128 / 256 = 0.5

Or for 3 fractional (decimal) digits, just compute 10^3 * remainder / denominator.

For floating point you would use a floating point library; it's far too complex to seriously consider rolling your own.

\$\endgroup\$
  • \$\begingroup\$ The result should be with 3 digits for the fractional part and least significant bit of that part should be rounded. So yes it is a fixed point format. \$\endgroup\$ – EMPV Nov 12 '15 at 19:25
  • 1
    \$\begingroup\$ correction: fraction = (2^8 * remainder)/denominator \$\endgroup\$ – greggo Nov 12 '15 at 20:24
  • \$\begingroup\$ How that's going to be represented in a register in the MSP430? \$\endgroup\$ – EMPV Nov 12 '15 at 21:28
  • \$\begingroup\$ That's up to you. \$\endgroup\$ – Brian Drummond Nov 12 '15 at 23:08
0
\$\begingroup\$

You can do

 q  =(num * 2^n)/ denom

as an integer division; the result will have n extra, fractional, bits. So with n=8,

(5*256)/2 = 640 

which is 2.5 * 256 or 2 * 256 + (128/256).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.