PDA

View Full Version : Multiplying large numbers



jhonea1
- 11th April 2006, 15:08
I think this is a quick question.

I need to raise numbers to the fifth power. Of course, this must be done with a series of multiplications since there is no exponent support in PBP.

The largest number possible in my equation would be 43. The max I could raise that to would be two (43*43=1849 which PBP can handle directly, 43^3=79507 which is greater than 16 bit).

I know that I'll have to use the DIV32 function after the calculation to get my answer reduced to something reasonable. That's easy enough.

So what I'm wondering is, is this "legal" in PBP?:

Dummy=43*43*43*43*43
Result=DIV32 12000


My confusion comes in the fact the the PBP documentation only gives examples of multiplying two numbers, then using the DIV32.

Thanks much,

Jason

paul borgmeier
- 11th April 2006, 15:43
Do you always divide by 12000?
How small can your number be?

Try doing your math in this order
Answer = 43 * 43 * 43 / 2 * 43 / 30 * 43 / 200

X var word
Y var word
Z var byte

X = Z* Z
Y = X * Z
X = Div32 2
Y = X * Z
X = Div32 30
Y = X * Z
X = Div32 200

For Z = 43, this gives 12250 (exact is 12250.70)

For Z = 20 this gives 266 (exact is 266.667)

For Z = 10 this gives 8 (exact is 8.333)

Good Luck,

Paul Borgmeier
Salt Lake City, Utah
USA

jhonea1
- 11th April 2006, 16:35
Thanks Paul

To answer your question, the number can be very small... less than 10. And there are actually two constants that I'll be dividing by - one is 12000 (120*100), the other is 600 (6*100).

I was giving the abbreviated story about what I am up to. What I'm actually calculating is a Taylor series expression for sin(theta). I'll write it out for the benefit of anyone else that happens upon this post:

sin(x)=theta -(theta^3)/3!+(theta^5)/5!...... with theta in radians

They can also check out this link that I have been refering to:
http://www.picbasic.co.uk/forum/showthread.php?t=718&highlight=arcsin

Of course, to calculate to any degree of accuracy, I have to scale numbers up (to get some significant figures) and then scale the final result down appropriately. My goal is two sig figs past the decimal point, with the last being rounded to either 0 or 5. Since that theta^5 is in there, the numbers get really difficult... especially when they are scaled up to begin with!

Since 3!=6, and I'm playing around with scaling, you can see where the 600 comes from. Same story for the 12000.

Anyhow, thanks so much... this will clean up my code nicely while improving accuracy.

Jason

Acetronics2
- 11th April 2006, 16:40
Hi, jhonea1

Depends upon the precision you need , but Have also a look there ...

http://www.awce.com/pak1.htm

and There :

http://www.micromegacorp.com/pbp.html

Alain

paul borgmeier
- 11th April 2006, 22:03
Sounds like a fun project and sounds like all is working ... however, as Melanie says in #19 of the link you posted, lookup tables are the fastest answer. I have used lookup tables with linear interpolation (for the added accuracy) as noted in here.

http://www.dattalo.com/technical/theory/sinewave.html


Have Fun,

Paul Borgmeier
Salt Lake City, Utah
USA

jhonea1
- 12th April 2006, 19:22
I just want to thank each of you for your help. I ultimately went with the Taylor series expansion method implementing Paul's suggestion to distribute the denominator across the terms.

FYI, this project is a senior design project for mechanical engineering, which is being presented next Tuesday. Though I have been working on it all semester, the last thing to complete was to calibrate the ADC value to a height based on the geometry of the mechanism. All along, I had been planning on a lookup table. But when I wrote it out, it required more space on my microcontroller than I had available.

That's when I started looking at Taylor series. By the time I got around to asking for your help, I was pretty well committed to that route. Given more time, I would probably use a different method, maybe even using a math coprocessor (which I admit I didn't even know existed until only a few days ago).

Now I can breathe a big 'ol sigh of relief knowing that it's done. All I have left to do is put it together on perfboard and put it in the enclosure.

Thanks again,

Jason

mister_e
- 14th April 2006, 17:41
I'll second and third the Lookuptable stuff. Sometimes time saving AND, of course, codespace is more important than messing with math. An external EEPROM is cheap and a table made in EXCEL or else can be easily done and dump in the EEPROM when saved in the correct format. I'd never thinking to use any of math processor as now. Maybe because what i do don't require it or i can't trust anybody else than me... but that's another story.

Good luck!