I was porting over a bit of code from Python to Arduino and came upon the following

in Python

(2-4)%5 yields 3

(4-2)%5 yields 2

on Arduino

(2-4)%5 yields -2

(4-2)%5 yields 2

So what am I missing here?

I was porting over a bit of code from Python to Arduino and came upon the following

in Python

(2-4)%5 yields 3

(4-2)%5 yields 2

on Arduino

(2-4)%5 yields -2

(4-2)%5 yields 2

So what am I missing here?

In python the modulo operator always yields a result with the same sign as the second operand, or zero.

So.... the short answer is "ain't gonna work"

Interesting problem and a bit surprising

Python match my HP48GX calculator

"ain't gonna work"

For some values of work.

You have to remember C's systems programming roots.

All well and good but doesn't solve my problem....

I'm working on a rock, paper,sicssors,lizard,spock idea and was staring with some python code that works Thus

diff=(player1-player2)%5

and where diff = 1 or 2 Player 1 wins

diff = 3 or 4 Player 2 wins

diff=0 tie

I guess I'll have to find a long winded if then solution

Bret:

in Python (2-4)%5 yields3

If it is so, it is really confusing.

Whandall:

If it is so, it is really confusing.

From what I've been reading this is what is expected from a mathematician point of view

Ok so here's what I've managed to work out...

From the thread at: How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers - Stack Overflow

The following works:

(diff+5)%5

Whandall:

If it is so, it is really confusing.

Think about clock arithmetic. (2-4)%12=10. 2 o'clock less 4 hours is 10 o'clock.

But -2 % 5 = **3** ?

Whandall:

But -2 % 5 =3?

On a 5 hour clock. Sure. 2 hours before 0 o'clock is...

Whandall:

But -2 % 5 =3?

5 goes into -2 0 times with a remainder of -2. So to me it should be -2.

OR 0 times 5 = 0

0 + (-2) = -2

Delta_G:

5 goes into -2 0 times with a remainder of -2. So to me it should be -2.OR 0 times 5 = 0

0 + (-2) = -2

Not disputing it.. Me I'm just trying to convert a python trick to Arduino and this solved it for me..

Note that in Python, -2/5 = -1

Math generally defines the remainder in integer division to be positive, see Euclidean division - Wikipedia

Taking the rice Python class? Me too!

Bret:

All well and good but doesn't solve my problem....diff=(player1-player2)%5

Try

```
diff=(player1-player2+5)%5
```

Think about it for a second, then slap your head and say "doh!".

Bret:

I'm working on a rock, paper,sicssors,lizard,spock idea ...

Dr. Spock or Mr. Spock?

PaulMurrayCbr:

Try

PaulMurrayCbr:

Try`diff=(player1-player2+5)%5`

As I posted above, that's what I did

Mr Spock.

There are two cycles in the group:

Scissors cuts paper, paper covers rock, rock crushes lizard, lizard poisons Spock, Spock smashes scissors

Scissors decapitates lizard, lizard eats paper, paper disproves Spock, Spock vaporizes rock, Rock blunts scissors

of course, there are subcycles. And there are some 7-element variants, but they are just silly.