division precision

diepenpj

New Member
Hello,

try to divide with a picaxe 40x1

how to get good results?

for example, if i try:

symbol pj= w0
symbol hulp1= w1
symbol hulp2= w2

main:
pj=6
hulp1 = pj/8
hulp2= pj//8
goto main

i get hulp1 = 0 and hulp2 = 6. I want an answer of 0.75 How can i achive this.

a second thing i want is like:

answer = 1 - (value1 divided by value2)

it seems that brackets aren't valid if i use picaxe mode 28x1.

thanks for any hints!
 

MORA99

Senior Member
If you need 0.75 you have to use a floating point chip.
Alternativly multiply everything, so you get 75 instead and do the conversion in your program logic.
 

lbenson

Senior Member
Because the picaxe uses only integer math (values of 0-255 for a byte value or 0-65535 for a word value) you must decide how many decimal places you want, and multiply your value by 10, 100, or 1000, to retain the values you want. When you display the value, your code must take into account where the decimal point is.

Regarding your question about the formula, answer = 1 - (value1 divided by value2), let's suppose that you are looking for a result that is less than one, good to two decimal places. For instance, if value1 is 6 and value2 is 8, you are looking for a result of .25.

The first problem is that the picaxe does math in strict left-to-right order, so you can't use brackets or parenthesis to indicate that you want to change that order. For the values given above, the following will display the answer you want.
Code:
b0 = 6
b1 = 8
b2 = b0 * 100 / b1
b2 = 100 - b2
b3 = b2/10
b4 = b2//10
sertxd(".",#b3,#b4)
Yes, it's convoluted, but that's the nature of decimal numbers with integer math. Many a pitfall lurks to make you come out with the wrong answer. Note that for this set of values, you could just say sertxd(".",#b2), but if your initial values were 19 and 20, you would get an answer of ".5" rather than ".05".
 
Last edited:

diepenpj

New Member
I understand what's told here, but what if b0 and b1 are large numbers, like 30999 and 33500? I'm using the formula for a hygrometer, that gives frequencies in this range, so it's not just theory.

could you please explain more how to do 1- (30999 / 33500)

many thanks!!!
 

tikeda

Member
Dividing the denominator by 'Z' is the same as multiplying the numerator by 'Z'
(Z*X)/Y = X/(Y/Z)

If you are only interested in values less <1, I would try:

100 - (30999/ (35500/100)) = 8

...which is equivalent to 0.08 and accurate to within about 1%.
 
Last edited:

papaof2

Senior Member
"I understand what's told here, but what if b0 and b1 are large numbers, like 30999 and 33500? I'm using the formula for a hygrometer, that gives frequencies in this range, so it's not just theory."

The Bx series of variables are BYTE variables and can only hold 0-255. For larger numbers, you must use the WORD variables (Wx series) which can hold up to 65535. Be aware that the WORD variables are each made up of two BYTE variables: W0 is made up of B0 and B1, so if you use W0 you can't use B0 or B1, or if you use either B0 or B1 then you can't use W0..

John
 

BCJKiwi

Senior Member
The other thing is that PICAXE does 32bit math!

For a look at all the techniques being discussed here plus 32bit math see examples in the code here;
http://www.picaxeforum.co.uk/showthread.php?t=8624

where a number of different techniques are discussed and demonstrated including;
Pre-caclulating constants
Scaling values before division,
modulus
32 bit math
 
Top