3 ms·
Without the low int the even/odd theorem falls apart for wrap around I've definitely seen algorithms that rely upon that. I would agree, whether error values a
by ratmice 10mo ago
Without the low int the even/odd theorem falls apart for wrap around
I've definitely seen algorithms that rely upon that.
I would agree, whether error values are in or out of band is pretty context dependent
such as whether you answered a homework question wrong, or your dog ate it.
One is not a condition that can be graded.
- School-Cotton 10mo agoWhat is the "even/odd theorem" ?
- ratmice 10mo agothat 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.
- xigoi 10mo agoIf you need wraparound, you should not use signed integers anyway, as that leads to undefined behavior.
- ratmice 10mo agoPresumably 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.
- School-Cotton 10mo agoSigned overflow being UB (while unsigned is defined to wrap) is a quirk of C and C++ specifically, not some fundamental property of computing.
- xigoi 10mo agoNim (the original one, not Nimony) compiles to C, so making basic types work differently from C would involve major performance costs.
- School-Cotton 10mo ago> 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.
- ratmice 10mo agoPresumably 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.
- Symmetry 10mo agoSpecifically, C comes form a world where allowing for machines that didn't use 2's compliment (or 8 bit bytes) was an active concern.
- Mond_ 10mo agoMeh, you also see algorithms that have subtle bugs because the author assumed that for every integer x, -x has the same absolute value and opposite sign. I view both of these as not great. If you strictly want to rely on wraparound behavior, ideally you specify exactly how you're planning to wrap around in the code.