How to divide 3 digit values; numbers with 2 significant figures results.

moorea21

Senior Member
My attached program is supposed to divide 2 integer values and use the result in another calculation.

The sorts of values for b1 and b2 that I tend to get are about 210 and 120 respectively, which is supposed to trigger the 'OK' message, but unless b2 is below about 85 it always says 'repeat'. As b4 = b1/b2, it's not likely to be an integer value... duh. I think its just a data type issue, but not sure

b4 needs to have 2 significant figures; I'm not familiar with picaxe basics odd arithmetic syntax; if b4 was 100 * (b1/b2), then b4 could just be an integer, but will basic actually divide like this, and use a non integer value during its calculation?

Or would I be better with a different data type?

Thanks,

M21
 

Attachments

techElder

Well-known member
There is only one "data type" in PICAXE: INTEGER.

There is an 8-bit INTEGER (decimal 0 - 255) and a 16-bit INTEGER (decimal 0-65535).
 

westaust55

Moderator
The usual method to in effect have say 2 decimal places is to multiply the upper line by 100 before the division.

The internal calculations are to 16-bits, so a given single line of maths, the intermediate result can exceed 255 (but not 65535)
Therefore a calculation such as 100 * 210 / 120 will work. Instead of 1.75 which PICAXE basic would truncate to 1
You will now have 175. This value is still less than 256 and thus can be stored in a byte variable.
 

moorea21

Senior Member
Thanks, having checked the code as a simulation (don't know why I missed that out when I wrote it, but there..) it was indeed truncating the quotient to 1, which I had no idea it would do. Didn't realise picaxe basic was so unfriendly to simple math...

In case I ended up with figures higher than 255, I decided to take this approach instead:-

b3=b1/b2
b4=b1*100/b2//100

And then assess b3 and b4 separately.
 

techElder

Well-known member
... realise picaxe basic was so unfriendly to simple math...
It isn't "unfriendly", moorea21, it just is what it is.

One must program with the tools at hand the way the tools work.

A hammer won't take a nut off a bolt, so we don't use a hammer to take a nut off a bolt.
 

srnet

Senior Member
Also be aware that ‘simple’ maths, as carried out by microcontrollers, may not give the results you expect.

For instance for byte variables;

0 – 1 = 255

255 + 1 = 0
 

moorea21

Senior Member
That's one I did know about, thanks for that. I'll make a point of reading a bit before assuming next time.
 
Top