IGNORED

# Best Way to determine if a variable is a multiple of 50

## Recommended Posts

What would be the best way to determine if a variable is a multiple of 50:

50, 100, 150, 200, 250

I planned on using code like so:

`if a = 50 || a = 100 || a = 150 || a = 200 || a = 250 then bla`

However i don't know if batari basic can handle such a large if statement or if the if statement would take too many cycles. Then i considered this:

`if a % 2 = 1 then bla`

However i know the atari 2600 isn't very good with muliplication or division. Any ideas?

Sincerely,

Open Source Pong

Edited by Open Source Pong
##### Share on other sites

What would be the best way to determine if a variable is a multiple of 50:
50, 100, 150, 200, 250

I planned on using code like so:

`if a = 50 || a = 100 || a = 150 || a = 200 || a = 250 then bla`

However i don't know if batari basic can handle such a large if statement or if the if statement would take too many cycles. Then i considered this:

`if a % 2 = 1 then bla`

However i know the atari 2600 isn't very good with muliplication or division. Any ideas?

Sincerely,

Open Source Pong

You can't put that many ors in a single if. The way I usually do something like that is to divide by 50 (or whatever), then multiply the answer by 50 to see if I get the same number:

```   include div_mul.asm
a = 55
b = 50 * (a / 50)
if a = b then goto a_is_multiple_of_50
rem * on a computer you would have to use the integer function
rem * b = 50 * int(a / 50)
rem * but batari Basic already returns integers```

Michael

##### Share on other sites

Doesn't quite work like i expected, my combo variable starts at 0 and then increments by 1 for each consecutively correct input until the player misses at which point it resets to 0. So:

```	rem divide the total combo by 50 then divide it by
rem 50 and see if it is a multiple of 50
temp1 = 50 * (combo / 50)

rem if it is a multiple of 50 and the player doesn't
rem have 7 lives already then give the player an
rem extra bar
if combo = temp1 && lives < 224 then lives = lives + 32```

Will multiply 0 by 50 and then try to divide it by 0 which will cause the game to give the player full lives when it first starts and after the player misses and the combo is reset to 0. Any ideas?

Sincerely,

Open Source Pong

Edited by Open Source Pong
##### Share on other sites

Multiplication and division are extremely slow on the 6507. It would be helpful to know *why* you need to know if something is a multiple of 50, so that a less general-purpose solution can be devised.

For example, if your values will never be higher than 250, and ROM space isn't critical, five comparison operations would be the fastest approach.

if a = 50 then gosub bla

if a = 150 then gosub bla

etc...

##### Share on other sites

For something like this, you only have 5 numbers to check, so I'd just use this seemingly unimaginative code:

```  if a=50 then bla
if a=100 then bla
if a=150 then bla
if a=200 then bla
if a=250 then bla```

If you place "bla" fairly close to these statements, it will take 30 bytes and anywhere from 8-37 cycles. If "bla" is far away from these statements, you're looking at 45 bytes and 11-40 cycles.

##### Share on other sites

For something like this, you only have 5 numbers to check, so I'd just use this seemingly unimaginative code:

```  if a=50 then bla
if a=100 then bla
if a=150 then bla
if a=200 then bla
if a=250 then bla```

If you place "bla" fairly close to these statements, it will take 30 bytes and anywhere from 8-37 cycles. If "bla" is far away from these statements, you're looking at 45 bytes and 11-40 cycles.

Alternatively, something like:

```lp:
if a>=50 then a=a-50:goto lp
' If a is zero, then we're happy```

In assembly language, such an approach can result in fast and compact code:

```  sec
lda _a
lp:
sbc #50
bcs lp
; Acc will equal zero (and Z flag will be set) if A was a multiple of 50.
; Carry will be set regardless.```

Worst-case time will be 36 cycles (5*5 for the branch repeats, plus 11). Nine bytes. Not sure how bB will do. Probably not that good, but not too totally horrible. Maybe something like:

```lp:
lda _a
cmp #50
bcc done
lda _a
sec
sbc #50
sta _a
jmp lp
done:
lda _a ; Zero if was a multiple of 50```

Eighteen bytes if there are no optimizations. I think 19 cycles per loop, plus 11, for a total of 106. Not nearly as good as the worst-case time for the assembly-code approach, but not totally horrible.

##### Share on other sites

I tried Supercat's code in bB:

```lp if a>=50 then a=a-50:goto lp
if !a then bla```

I get 20 bytes, but 12-112 cycles. So what to use depends on if you prefer space over cycles.

Edited by batari
##### Share on other sites

Small Problem:

```	rem if the player has a 50, 100,
rem 150, 200, or 250 combo streak
rem then give him a life
if combo = 50 && lives < 228 then lives = lives + 32
if combo = 100 && lives < 228 then lives = lives + 32
if combo = 150 && lives < 228 then lives = lives + 32
if combo = 200 && lives < 228 then lives = lives + 32
if combo = 250 && lives < 228 then lives = lives + 32```

Does what i want with one hitch. It maxes the player's lives at 7 instead of just giving him just one. Any ideas why?

Sincerely,

Open Source Pong

Edited by Open Source Pong
##### Share on other sites

A-HA! So that's what you're doing. In that case, why not just:

`if combo = nextbonus then nextbonus = nextbonus + 50: bla`

Where nextbonus is initialized to 50 when the game starts.

Or, if the combo count isn't actually displayed or used anywhere else:

`if combo = 50 then combo = 0: bla`

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.