Results 1 to 3 of 3

Math Help - Real number division using integers

  1. #1
    Newbie
    Joined
    Jul 2011
    Posts
    2

    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
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member kalyanram's Avatar
    Joined
    Jun 2008
    From
    Bangalore, India
    Posts
    142
    Thanks
    14

    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.
    Last edited by kalyanram; July 9th 2011 at 10:32 PM. Reason: Error in notation
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Jul 2011
    Posts
    2

    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
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Proofs using real numbers and integers
    Posted in the Advanced Math Topics Forum
    Replies: 2
    Last Post: September 8th 2011, 06:58 PM
  2. Division of a real analytic function
    Posted in the Differential Geometry Forum
    Replies: 1
    Last Post: March 9th 2011, 10:58 AM
  3. Find the number of integers
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: November 30th 2009, 06:55 PM
  4. roots real integers
    Posted in the Math Topics Forum
    Replies: 2
    Last Post: August 21st 2009, 08:06 PM
  5. number of ways to add integers
    Posted in the Statistics Forum
    Replies: 4
    Last Post: July 26th 2008, 04:45 PM

Search Tags


/mathhelpforum @mathhelpforum