# Thread: Real number division using integers

1. ## Real number division using integers

I need to divide an integer by a real number with the following constraints or conditions...

Integer value is in the range 0 to 1023
Real Divisor value is in the range 50 to 500 with 2 decimal precision
16bit Integer calculations only (maximum 65535)
Output precision = 2 decimal place
The output is used only for display (not further calculations)
The divisor is multiplied by 100 at start

Using basic programming with 16bit integer...

div = 4144 ' Enter data in integer form (41.44*100)
ain0 = 450 ' Integer value to divide
voltint = (ain0 / div) * 100 ' This gives the integer output (Left of dp)

Tmp1 = ain0 * 10 / div * 1000 '
The above line gives 0 due to division part of the equation results in a value less than 1 before multiply by 1000.

Voltdp = Tmp1 - (voltint * 100) ' this line removes the integer portion before display value for right of dp.

Correct result would be 10.85

Any suggestions on how to obtain the value for the right of Decimal Point considering the constraints mentioned.

Thanks
Toni

----------------------------------
complete subroutine
Display volt:
div = 4144 ' Scale factor (41.44*100)
ain0 = 450 ' Integer value to divide
Voltint = (ain0 / div) * 100 ' Build integer part
Tmp1 = ain0 * 10 / div * 1000 ' Build fractional part
Voltdp = Tmp1 - (Voltint * 100) ' Remove int

Print #voltint, "."
{code here for 0 insertion}
Print #voltdp, "V "
Return

2. ## Re: Real number division using integers

Hi,
Form the way you wrote your algorithm I assume that your 16-bit processor has the ability to give the integer part of the quotient of a division if N(numerator) and D(denominator) are both integers. If that is true here is how I go about doing the division.
Step1. Take D in integer.
Enter D in integer by multiplying with suitable 10's power. (Here it is 100)
Real Divisor value is in the range 50 to 500 with 2 decimal precision
We handle values of 0<N<655 will be contained in 16 bits after multiplication with 100.
Step2. if (N <= 655)
{
N = N * 100;
Qi = (N / D);(integer part of quotient)
R = N - Qi * D; (remainder)
R = R * 100;
Qf = (R / D);(fractional part of quotient)
}
We handle values of 655<N<1023 after halving N and D values as then we can represent the values in 16-bits and as we have divided both N and D by 2 our result is still accurate enough.
Step3. if (N > 655)
{
N = N/2;
D = D/2;

N = N * 100;
Qi = (N / D); (integer part of quotient)
R = N - Qi * D; (remainder)
R = R * 100;
Qf = (R / D);(fractional part of quotient)
}

We can represent the quotient as an integer as below
Q = Qi.Qf;

Rounding to two decimal places of Qf needs extracting them from the result recursively.

Kalyan.

3. ## Re: Real number division using integers

Thank you Kalyan for the help
I could not get correct results of the second method you suggested. { if (N <= 655) }

However, I am now using floating point addon for the program language & a time check of the compiled code on the simulator shows 138.8us for the following lines
PWM_pc = 75.54
Pwm_val = (1023 * PWM_pc) / 100
& I just use the inbuilt decimal format for output

So Thanks again for your help

regards

Toni