Maths Moron needs some pointers

Blazemaguire

Senior Member
Hello,

This is my first forage into Picaxe maths, and I'm struggling to understand what is happening - I've read manual 2 regards variable maths, but It doesn't seem to explain in enough detail to cover my issue below.

Hoping you can shed some light.

(I won't bore you with the back story of why I'm doing it, but it's in essence the starting point for working out velocity based on number of input pulses recieved in given amount of time) I'm trying to do some simple remainder and quotient maths, and just returning the answers into word variables that i'm checking via the sertxd command via simulation.

My code is:


main:

w1= 3
w2= 7

w3= w1/w2 ' should return quotient into w3

w4=w1//w2 ' should return remainder into w4


sertxd (#w3," Quot ",#w4," Rem") 'write word variables to serial terminal
pause 1000

w3=0 ' reset word variables
w4=0

goto main



In 'normal' maths, this should retain a quotient of '0' and a remainder of 428..... etc - Yet it returns qoutient ='0' and remainder = '3' on the serial window. - My question is, why do I get '3' for the remainder? - that is neither rounded up or down!? - I was expecting '4'... - I can't progress my maths any further, until I truly understand why this is happening.

There is obviously some reason for this, but I don't understand enough about PICAXE maths to know why this is... unless I'm looking in the wrong place, or just plain stupid, I can't see anything in manual 2 that explains why this is. - Can anyone shed any light? - Maths is not my strong suit on a good day, let alone the weird maths workarounds you have to do with micro controller maths.

Any pointers to get me started would be really appreciated!

Thanks


Rob
 

Blazemaguire

Senior Member
In the simplest terms I can put it Rossko57 (and remembering I'm a maths idiot, tread gently!) - If I divide 3 by 7 on a calculator, I get 0.428 in the real world. I was expecting 0 for quotient and then something close to 428.... etc as remainder. - Unless my understanding of what a 'remainder' is is totally wrong (as, it seems it is!)

If this is the case, can someone, please give me some GCSE maths lessons to explain why 3 is the answer and not 428... as I expected? - Like I said, Maths is not my strong suit. - But I want to learn....


I'll be back when I've read up on quotient/remainder...seems I've totally misunderstood what they difference is! (I am 3 beers and a glass of wine down tonight, so give me a chance!)
 

Blazemaguire

Senior Member
Like I said, Maths Moron! Seems in my mind, the quotient was the part on the left of the decimal point, and remainder was the part on the right. - I now know that's not the case... which means I'm back to the drawing board with this particular problem - Slightly embarrassing, but I guess we learn. - I'm now very angry at myself for not knowing something that is apparently taught in Junior school according to my wife! ... I won't tell you what I do for a living.
 

Circuit

Senior Member
PICAXE cannot compute floating point arithmetic - it is integer only. Hence the result that you are getting. You need a floating point unit in order to handle the type of arithmetic you require. There are workarounds available; multiplying by orders of ten then dividing etc. to keep the numbers as integers. Have a look at the floating point chip FPU001 at Techsupplies. Also, plenty on this forum using the workarounds.
 

lbenson

Senior Member
This will give you the number you were expecting for this particular example.

w4=w1*1000//w2

For other examples, you must make sure that multiplying w1 by 1000 will not exceed the maximum value a word variable can hold, 65535

Other cautions regarding positive integer maths, and word and byte size apply.
 
Top