Integer Math Tips


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2010
    Posts
    19

    Question Integer Math Tips

    I am finding myself involved in projects that require math operations, but I am not yet accustomed to working with integer math.

    For example, I want to compute the value of a resistor R2 in a voltage divider setup. The value is given by:

    R2 = R1 / ((Vin/Vout)-1)

    Where R1 is the resistor connected to +Vin, R2 is the unknown resistor connected to GND, Vin = 5V= 1023 in 10bit ADC resolution and Vout = ADC reading

    What would be the best way to compute R2 with the least loss of precision?
    (Assume that the value of R1 is optimally chosen)

    BTW, I don't want to use a floating point math library because of the overhead (I need the extra space)

  2. #2


    Did you find this post helpful? Yes | No

    Default

    Hello

    Take a look at this it will show you how to approach your problem.

    http://www.picbasic.co.uk/forum/show...ghlight=quanta

    Also search the forum for "quanta"

    Regards
    Mark
    Last edited by mark_s; - 26th August 2010 at 00:27.

  3. #3
    Join Date
    Jul 2010
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    Thank you Mark. I had been looking at those posts before I posted my question to the forum, and I don't actually follow some of the concepts.

    Looking at all posts retrieved with a Quanta search didn't help much either, as most posts have code where the user seems to know how to perform the quantization but offers no explanation.

    Lets take the first division, Vin/Vout, the problem I am finding is as follows:

    Since Vin is 5V or 1023 in ADC scale values, I need to do 1023/Vout. To get a 2 decimal precision I would need to do ( 1023*100/Vout ) / 100, but multiplying 1023*100 is already above 65535. Now, the internal multiplication result will be in 32 bits, so I thought of using Div32:

    Vin = 1023 * 100
    temp = Div32 Vout

    but that doesn't work if Vout is a low number, because the value of temp would exceed 65535.

    So at best I can do:

    Vin = 1023 * 64
    temp = Div32 Vout

    But that doesn't seem good to me either.

    Any ideas?

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by dream- View Post
    Any ideas?
    Hi,

    One VERY good idea ...

    Have a look to your manual and read about the REAL limit of DIV32 Numerator ...

    Now ... may be your measuring method is " not so good " for a large range of values ???

    Alain
    Last edited by Acetronics2; - 26th August 2010 at 13:28.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  5. #5
    Join Date
    Jul 2010
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    Hi Alain,

    Bear with me, I am a beginner

    I have read the DIV32 entry from the manual, that's how I learned about DIV32 in the first place. It says it can divide a 31bit number by a 15bit number, that means I could make the numerator very large, but he problem is that since the value of Vout (the denominator) can be as low as 1, then the result would not fit into a WORD variable. Therefore the maximum value for the numerator has to be 65535, which is what I got in my example. (well 65535 is technically 1024*64, but you get the point)

    Vin = 1023 * 64
    temp = Div32 Vout

    Maybe you mean something I am not seeing, if so please let me know, I am new to this stuff.

    Also I am not sure what you mean about my measuring method? All I want is to compute the unknown resistance from a voltage divider. But that should be irrelevant, shouldn't it? I mean, the problem here is just an integer arithmetic one.

    I appreciate your help guys.
    Last edited by dream-; - 26th August 2010 at 17:34.

  6. #6
    Join Date
    Jul 2010
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    Alain,

    I think I understand what you mean with the comment about the measurement range. Maybe I could just make two cases or simply make Vout have a range from say 3 to 1023. That way I could really scale up the numerator and still have a result within the bounds of a WORD.

  7. #7
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Here's one of the best sites I know of for tips & tricks working with integer math on small systems: http://www.emesystems.com/BS2index.htm
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  8. #8
    Join Date
    Jul 2010
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    Thank you Bruce, that page has a wealth of information!

    There is even a section on my particular problem of division where the denominator is a variable.

    Let's see how they do it...

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts