Earlier quoted context omitted.
In what languages is n % 2 -1 for negative odd numbers? Edit: apparently JS, java, and C all do this. That’s horrifying
Horrifying? It’s mathematically correct.
Test if a number is even
61–68 of 68 posts
Re: Test if a number is even
#62Earlier quoted context omitted.
In what languages is n % 2 -1 for negative odd numbers? Edit: apparently JS, java, and C all do this. That’s horrifying
Horrifying? It’s mathematically correct.
Re: Test if a number is even
#63Earlier quoted context omitted.
Better to use TCP, but I like your approach.
- Attempt to factor your integer n into primes... - Once you have the complete prime factorization, check whether 2 is among its prime factors... - If 2 is a factor, it’s even; if not, odd.
Also I only use the step over command in the debugger
Re: Test if a number is even
#64Earlier quoted context omitted.
In what languages is n % 2 -1 for negative odd numbers? Edit: apparently JS, java, and C all do this. That’s horrifying
Horrifying? It’s mathematically correct.
https://stackoverflow.com/questions/13683563/whats-the-diffe...
Re: Test if a number is even
#65Re: Test if a number is even
#66Earlier quoted context omitted.
Horrifying? It’s mathematically correct.
It's actually really awkward. Math usually considers (-7 mod 5) === (2 mod 5). But in C, (-7 % 5 != 2 % 5).
Think of a clock which is a ring of size 12. In a clock, going backwards 15 hours (-15) is the same as going backwards 3 hours (-3) which is the same as going forwards 9 hours.
-15 = -3 = 9 modulo 12
Re: Test if a number is even
#67Earlier quoted context omitted.
It's actually really awkward. Math usually considers (-7 mod 5) === (2 mod 5). But in C, (-7 % 5 != 2 % 5).
No. Math considers -7 = 3 modulo 5. it's a ring that repeats every 5 units. -7 + 5 + 5 = 3. Think of a clock which is a ring of size 12. In a clock, going backwards 15 hours (-15) is the same as going backwards 3 hours (-3) which is the same as going forwards 9 hours. -15 = -3 = 9 modulo 12
Math usually considers (-7 mod 5) === (3 mod 5). But in C, (-7 % 5 != 3 % 5).
The issue is that -7 and 3 are congruent, but the % operator keeps the sign. So -7 % 5 yields -2, not +3. Those are congruent, but not equal. I've never had a use for this behaviour, but I've definitely had to work around it. The lazy way is ((x % n) + n) % n which is safe (assuming n > 0).
Re: Test if a number is even
#68Earlier quoted context omitted.
No. Math considers -7 = 3 modulo 5. it's a ring that repeats every 5 units. -7 + 5 + 5 = 3. Think of a clock which is a ring of size 12. In a clock, going backwards 15 hours (-15) is the same as going backwards 3 hours (-3) which is the same as going forwards 9 hours. -15 = -3 = 9 modulo 12
You are correct. I thinko'd and missed the edit window. I meant to say: Math usually considers (-7 mod 5) === (3 mod 5). But in C, (-7 % 5 != 3 % 5). The issue is that -7 and 3 are congruent, but the % operator keeps the sign. So -7 % 5 yields -2, not +3. Those are congruent, but not equal. I've never had a use for this behaviour, but I've definitely had to work around it. The lazy way is ((x % n) + n) % n which is s…