Need a couple math routines
Oldbitcollector (Jeff)
Posts: 8,091
I've started using byte increments for calculating time in my game loops.
This works, but it would be handy to be able to perform functions based on timing.
Can someone help with a couple routines for determining if the byte is:
* an even number. (true/false)
* divisible by 5. (true/false)
Thanks
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
This works, but it would be handy to be able to perform functions based on timing.
Can someone help with a couple routines for determining if the byte is:
* an even number. (true/false)
* divisible by 5. (true/false)
Thanks
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
Comments
IF Value & 1 ' succeeds if odd
IFNOT Value & 1 ' succeeds if even
or
TEST Value,#1 wz ' zero true if even
TEST Value,#1 wc ' carry true if odd
Post Edited (Mike Green) : 2/11/2010 3:10:27 PM GMT
Range is 0 - 254 in spin.
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
Note this line at bottom of code on OP: 'Reamainder is what left in arg1
Spin is easy: ifnot number // 5 ' if true number is evenly divisible
Here's an interesting link: mathforum.org/dr.math/faq/faq.divisibility.html
Post Edited (jazzed) : 2/11/2010 5:15:32 PM GMT
Duh.. (head smack) My brain seems to default to decimal numbering. So a test routine might look like..
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
That "last digit is 5 or 0" test only works in decimal! Your code fails if number is 10, e.g.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
· Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
· Prop Tools under Development or Completed (Index)
· Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
· Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
You're right. I was half asleep [noparse]:)[/noparse]
[noparse][[/noparse]Edit] -- I just saw that [noparse][[/noparse]another version of] this solution was previously posted.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
Post Edited (JonnyMac) : 2/11/2010 7:14:24 PM GMT
if (num8bits * $33) & $FF == 0 'if a number under 256 is divisible by 5 ...
or
if (num16bits * $3333) & $FFFF == 0 'if a number under 65536 is divisible by 5 ...
In assembly, this is done on LONGS with 16 shifts and 16 adds, and the result long = 0 if it divides 5 with zero remainder.
(or just one MUL, if we had one)
I am not sure whether this trick works with Spin variables defined as bytes or words, because it is an overflow trick.
I also don't know if it has any advantage over // if the remainder is re-normalized by a division by the "hex 3" nybbles.
This may fail on the number 5. If so, I would try fixing it by adding one to the variable being tested product.
I haven't actually tried it, so it may not work at all.
Bocephus method seems like a similar concept.
Post Edited (VIRAND) : 2/11/2010 10:02:37 PM GMT
OBC
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?
Visit the: The Propeller Pages @ Warranty Void.
jim
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Signature space for rent!
Send $1 to CannibalRobotics.com.
But I did a check for the last 4 bits and the pattern repeat after 16.
so binary-and the number with %1111
check if result is any of the 16 below.
Is 0/5·valid?, if not check if start value <>0 as 80 have·same 4 bits as zero
0101 5
1010 10
1111 15
0100 20
1001 25
1110 30
0011 35
1000 40
1101 45
0010 50
0111 55
1100 60
0001 65
0110 70
1011 75
0000 80
0101 85 < repeat
1010 90
1111 95
0100 100
1001 105
1110 110
0011 115
1000 120
Post Edited (tonyp12) : 2/12/2010 3:05:48 AM GMT
and 4 only possible with 2 bits
could be so simple as: if(number and%11)== 0 || 5 || 10 || 15
00 0
01 5
10 10
11 15
00 20
01 25
10 30
11 35
00 40
01 45
10 50
11 55
00 60
01 65
10 70
11 75
There are 51 multiples of 5 between and including 0 and 255.
There must be a solution involving the 2 lowest bits but I didn't think of that.
Obviously the lookup table is going to be
%100001000010000............................1000010000100001
and is probably pointless to lookup when num//5==0 is available.
Both bit and byte lookup tables waste memory and one wastes time except in PASM.
Problem with the two lowest bits is that they are not exclusive.
05 => #0101 for last two bits also 1, 9, 13
10 => #1010 for last two bits also 2, 6, 14
15 => #1111 for last two bits also 3, 7, 11
@Virand
There are only 25 multiples of 10, 4 multiples of 50 and so on. What if you multiplied by 2 then chopped the number down the number to 3 bits. If it's a multiple of 5 then those are 000.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Signature space for rent!
Send $1 to CannibalRobotics.com.
if no > 130 no = no - 130
if no = 0 then multiple of 5
if no > 65 no = no - 65
if no = 0 then multiple of 5
if no > 35 no = no - 35
if no = 0 then multiple of 5
if no > 20 no = no - 20
if no = 0 then multiple of 5
if no > 10 no = no - 10
if no = 0 then multiple of 5
if no > 5 no = no - 5
if no = 0 then multiple of 5