Large Numbe Maths

goom

Senior Member
I am attempting to modify the response of an RC sevo to give a non-linear response (e.g a square law relationship between the PWM from the receiver and the PWM to the servo).
I am having some difficulty within the confines of integer maths and word variable length. I understand that the actual calculations within a single line of code uses a double word, so would the following work:
W1=W0*W0*80/10000
Where W0=380-300*100/80 (=100)
And the result for W1 should be 80

Thanks in advance for any help.

Kevin


 
 

Jeremy Leach

Senior Member
Hi Kevin

Yes , I think it should. Because with the value you've chosen for W0 the expression for W1 fits in a double word while it's calculating, giving a single word result.

W1=W0*W0*80/10000

For this expression in general though, it could overflow the double word if W0 was too high . ie W0 * W0 * 80 > [(65536 * 65536) - 1] , ie W0 > 7327.


Edited by - Jeremy Leach on 13/04/2006 07:40:42
 

Jeremy Leach

Senior Member
Should also say that for :

W1=W0*W0*80/10000

It's good practice to reduce fractions manually if possible:

i.e better to have:
W1=W0*W0*8/1000

Which will help avoid overflow.
 

hippy

Technical Support
Staff member
Internally the PICAXE does its maths only on a single 16-bit accumulator, so any intermmediate calculation over 65535 ($FFFF) will overflow.<code><pre><font size=2 face='Courier'>- w0 = $1000 * $1000 / $1000
- w1 = $1234 * $100 / $100
- sertxd (#w0,&quot; &quot;,#w1,CR,LF) </font></pre></code> Prints &quot;0 52&quot; ( $00 $34 ) whereas double word internal processing would have given back the $1000 and $1234 values unchanged.
 

Jeremy Leach

Senior Member
Ah ....I'm sorry for misleading people then. I thought it didn't work like that.

But manual says that : <i>When multiplying two 16 bit word numbers the result is a 32 bit (double word) </i>

And you can extract the highest word using **

So, ** allows you to access the overflow of a multiplication, but this overflow isn't used in intermediate processing of an expression?

Edited by - Jeremy Leach on 13/04/2006 10:33:30
 

Dippy

Moderator
What you say is true Jeremy, but beware anyone thinking that you can do a whole maths sum in 32 bit.
It doesn't work like that.
So, to any observers, PLEASE don't imagine that:
2000 * 50000 / 2000 will end in 50000 - it won't. (It'll perform the /2000 part based on the lower 16 bits of the previous part).

So, best to 'think' 16 bits all the time unless you really need to manipulate the top ** 16 bits.
 

hippy

Technical Support
Staff member
<i>But manual says that : When multiplying two 16 bit word numbers the result is a 32 bit (double word)

So, ** allows you to access the overflow of a multiplication, but this overflow isn't used in intermediate processing of an expression? </i>

I can see how the impression of full 32-bit maths arose.

For all but multiply, maths is 16-bit only.

For '*' and '**' the result is 32-bit but imediately truncated to 16-bits before the value is stored or operated on further.

For '**' the top 16-bits are moved down to the lower prior to truncation.
 

goom

Senior Member
Jeremy, hippy, thanks for your help.
If I just turn the equation around:
W1=W0*W0/1000*80
Where W0=380-300*100/80 (=100)
This should work?
I cannot reduce the fraction as suggested since the numbers in the equation are actually variables (pulse widths from an RC receiver with a double speed, 8MHz, clock)
Also, I cannot work with smaller numbers since the rounding errors with integer maths becomes too large with smaller values of W0 (e.g. if I divided W0 by 10 before entering it into the equation).
I'll just have to try it to be sure.
The whole idea is to get a selectable and variable non-linear PWM output from a linear PWM input. I intend to use it for the rudder control of a model saiboat where one needs fine control when going in a straight line, but full rudder movement when making abrupt changes in direction.
Is there any way to share code in this forum (apart from posting in the body of a message)? I have written and implemented code for a servo slowdown and 4-channel RC switch for example, and would be pleased to share it if there is interest. A &quot;code library&quot; would be a wonderful addition to a great forum.

 
 

Dippy

Moderator
You're right, a code-library or even links to a My Favourite Code page would be nice. People have mentioned it many many times.

But, as you say, try it. Why not?
 
Top