PDA

View Full Version : Integer Math Tips



dream-
- 25th August 2010, 23:10
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)

mark_s
- 26th August 2010, 00:25
Hello

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

http://www.picbasic.co.uk/forum/showthread.php?t=712&highlight=quanta

Also search the forum for "quanta"

Regards
Mark

dream-
- 26th August 2010, 07:40
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?

Acetronics2
- 26th August 2010, 13:18
Any ideas?

Hi,

One VERY good idea ...

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

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

Alain

dream-
- 26th August 2010, 17:31
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.

dream-
- 26th August 2010, 17:50
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.

Bruce
- 26th August 2010, 18:14
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

dream-
- 26th August 2010, 19:27
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...