zlacker

[parent] [thread] 13 comments
1. umanwi+(OP)[view] [source] 2025-12-05 14:48:25
What is the "even/odd theorem" ?
replies(1): >>ratmic+44
2. ratmic+44[view] [source] 2025-12-05 15:07:10
>>umanwi+(OP)
that all integers are either even or odd, and that for an even integer that integer + 1 and - 1 are odd and vice versa for odd numbers. That the negative numbers have an additional digit from the positive numbers ensures that low(integer) and high(integer) have different parity. So when you wrap around with overflow or underflow you continue to transition from an even to odd, or odd to even.
replies(1): >>xigoi+k7
◧◩
3. xigoi+k7[view] [source] [discussion] 2025-12-05 15:22:01
>>ratmic+44
If you need wraparound, you should not use signed integers anyway, as that leads to undefined behavior.
replies(2): >>ratmic+w9 >>umanwi+ca
◧◩◪
4. ratmic+w9[view] [source] [discussion] 2025-12-05 15:31:12
>>xigoi+k7
Presumably since this language isn't C they can define it however they want to, for instance in rust std::i32::MIN.wrapping_sub(1) is a perfectly valid number.
replies(1): >>xigoi+Ia
◧◩◪
5. umanwi+ca[view] [source] [discussion] 2025-12-05 15:34:06
>>xigoi+k7
Signed overflow being UB (while unsigned is defined to wrap) is a quirk of C and C++ specifically, not some fundamental property of computing.
replies(2): >>xigoi+Qa >>Symmet+ui
◧◩◪◨
6. xigoi+Ia[view] [source] [discussion] 2025-12-05 15:36:13
>>ratmic+w9
Nim (the original one, not Nimony) compiles to C, so making basic types work differently from C would involve major performance costs.
replies(1): >>beagle+Bn1
◧◩◪◨
7. xigoi+Qa[view] [source] [discussion] 2025-12-05 15:36:30
>>umanwi+ca
Nim (the original one, not Nimony) compiles to C, so making basic types work differently from C would involve major performance costs.
replies(2): >>umanwi+ud >>ratmic+Re
◧◩◪◨⬒
8. umanwi+ud[view] [source] [discussion] 2025-12-05 15:47:00
>>xigoi+Qa
> making basic types work differently from C would involve major performance costs.

Not if you compile with optimizations on. This C code:

  int wrapping_add_ints(int x, int y) {
      return (int)((unsigned)x + (unsigned)y);
  }
Compiles to this x86-64 assembly (with clang -O2):

  wrapping_add_ints:
          lea     eax, [rdi + rsi]
          ret
Which, for those who aren't familiar with x86 assembly, is just the normal instruction for adding two numbers with wrapping semantics.
◧◩◪◨⬒
9. ratmic+Re[view] [source] [discussion] 2025-12-05 15:52:34
>>xigoi+Qa
Presumably unsigned want to return errors too?

Edit: I guess they could get rid of a few numbers... Anyhow it isn't a philosophy that is going to get me to consider nimony for anything.

◧◩◪◨
10. Symmet+ui[view] [source] [discussion] 2025-12-05 16:06:05
>>umanwi+ca
Specifically, C comes form a world where allowing for machines that didn't use 2's compliment (or 8 bit bytes) was an active concern.
replies(2): >>aw1621+5x >>Asooka+0G1
◧◩◪◨⬒
11. aw1621+5x[view] [source] [discussion] 2025-12-05 17:05:12
>>Symmet+ui
Interestingly, C23 and C++20 standardized 2's complement representation for signed integers but kept UB on signed overflow.
◧◩◪◨⬒
12. beagle+Bn1[view] [source] [discussion] 2025-12-05 21:06:13
>>xigoi+Ia
And yet, Nim does overflow checking by default.
◧◩◪◨⬒
13. Asooka+0G1[view] [source] [discussion] 2025-12-05 22:51:38
>>Symmet+ui
Back when those machines existed, UB meant "the precise behaviour is not specified by the standard, the specific compiler for the specific machine chooses what happens" rather than the modern "a well-formed program does not invoke UB". For what it is worth, I compile all my code with -fwrapv et. al.
replies(1): >>aw1621+3l2
◧◩◪◨⬒⬓
14. aw1621+3l2[view] [source] [discussion] 2025-12-06 05:33:05
>>Asooka+0G1
> UB meant "the precise behaviour is not specified by the standard, the specific compiler for the specific machine chooses what happens"

Isn't that implementation-defined behavior?

[go to top]