1/15/2025 at 4:09:29 AM
It may be worth pointing out: these are equivalent comparisons when testing for even numbers but cannot be extrapolated to testing for odd numbers. The reason being that a negative odd number modulus 2 is -1, not 1.So `n % 2 == 1` should probably [1] be replaced with `n % 2 != 0`.
While this may be obvious with experience, if the code says `n % 2 == 0`, then a future developer who is trying to reverse the operation for some reason must know that they need to change the equality operator not the right operand. Whereas, with `n % 1 == 0`, they can change either safely and get the same result.
This feels problematic because the business logic that necessitated the change may be "do this when odd" and it may feel incorrect to implement "don't do this when even".
I really disfavor writing code that could be easily misinterpreted and modified in future by less-experienced developers; or maybe just someone (me) who's tired or rushing. For that reason, and the performance one, I try to stick to the bitwise operator.
[1] Of course, if for some reason you wanted to test for only positive odd numbers, you could use `n % 2 == 1`, but please write a comment noting that you're being clever.
by venning
1/15/2025 at 5:32:47 AM
I really disfavor writing code that could be easily misinterpreted and modified in future by less-experienced developersThat's their problem. Otherwise you're just contributing to the decline.
by userbinator
1/15/2025 at 6:30:12 AM
In what languages is n % 2 -1 for negative odd numbers?Edit: apparently JS, java, and C all do this. That’s horrifying
by notfish
1/15/2025 at 1:20:43 PM
Horrifying? It’s mathematically correct.by jagged-chisel
1/16/2025 at 11:31:56 AM
it's a semantics problem, not a maths problem - modulus and remainder are not the same operation. This easily trips up people since `%` is often called "modulo", yet is implemented as remainder operation in many languageshttps://stackoverflow.com/questions/13683563/whats-the-diffe...
by seritools
1/15/2025 at 8:14:23 PM
It's actually really awkward. Math usually considers (-7 mod 5) === (2 mod 5). But in C, (-7 % 5 != 2 % 5).by michael1999
1/18/2025 at 7:49:09 AM
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
by arcastroe
1/21/2025 at 11:14:42 PM
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 safe (assuming n > 0).
by michael1999
2/2/2025 at 5:14:53 AM
+1. I wholeheartedly agree. That "lazy way" looks all too familiar haha.by arcastroe
1/15/2025 at 8:25:12 PM
wrong. it's not any more correct than 1. that's the key part of an "equivalence" class is that the elements are "equivalent"by affinepplan
1/15/2025 at 4:28:06 AM
You can just not( iseven() )by neuroelectron