4 ms·
It's modular arithmetic because of the internals of the processor, not because of some law-of-the-universe. It could just as easy a processor not allow over- o
by jimktrains2 11y ago
It's modular arithmetic because of the internals of the processor, not because of some law-of-the-universe.
It could just as easy a processor not allow over- or under- flow operations. However, it's easiest to just truncate the number and essentially do modular arithmetic. That's the difference. Over- or under- flow, checked, wrap-around all refer to the state at which the operation's answer needs more space than it is given. Modular arithmetic and checked instructions are what to do in those situations.
Aside, division in modular arithmetic can be done, but it's not what most people want (hence why usually not performed). 4 / 3 mod 7 -> 4 * 5 mod 7 ( 5 is the multiplicative inverse of 3 mod 7 ( 3 * 5 = 1 mod 7) -> 6 mod 7
- vog 11y ago> It's modular arithmetic because of the internals of the processor, not because of some law-of-the-universe I beg to differ. There were various integer representations tried, such as having a separate "sign" bit on integers. But the two's complement finally was used by everyone. So there's something deeply unique to modulo arithmetics that none of the other representations could offer. This is not just about saving a few gates in the circuit. > However, it's easiest to just truncate the number and essentially do modular arithmetic The point is, there is no truncatenation step. It's just a side effect. If you build any naive adder circuit, substractor, multiplicator - even if you totally ignore negative numbers, you automatically have a full working two's complement, that works for negative numbers without any additional effort. And why is that? Because the mathematical structure behind it is so dead-simple that it almost works by miracle. (except for division, which is highly non-intuitive and has more pitfalls than usual pitfall of division by zero.) As a consequence of this simple math structure, it is very easy to build in hardware without any additional handling of nasty special cases.
- jimktrains2 11y ago> As a consequence of this simple math structure, it is very easy to build in hardware without any additional handling of nasty special cases. Which is exactly my point! It's only the way we build things, not some hard-and-fast way things work intrinsically. I have no argument with it's an extremely simple way to deal with an over- or under- flow. But that's my point! It's a way to deal with the case, not the case itself!
- jimktrains2 11y agoI guess I missed my edit window: > except for division, which is highly non-intuitive and has more pitfalls than usual pitfall of division by zero. Division literally works the same way it does normally: 10 / 2 = 5 => 5 subtractions of 2 from 10 gives 0. 0: 10 1: 8 2: 6 3: 4 4: 2 5: 0 4 / 3 = 6 mod 7 => 6 subtractions of 3 from 4 gives 0 0: 4 1: 1 2: 5 3: 2 4: 6 5: 3 6: 0 I wouldn't say it's fraught with pitfalls, just that it's not common to see. FWIW, in an under- or overflow- situation we normally don't want modular arithmetic, hence why we still attempt to trap instead of ignore it.
- vog 11y agoI was referring to the non-prime modulo values, such as 2^32 or 2^64. In those cases, you do not only have "well-defined" and "division by zero", but also "division with multiple valid results". More generally, this is the issue of zero divisors. [1] Modulo 2^n, you have proper division only if you divide by odd numbers, then you have division by zero, and finally you have division by any other even number, which is very different from what people used to. There may be no solution, or two solutions, or many more solutions. Yes, could can still solve them by reducing the modulo, but that's the extra pitfall that you don't have with plain integers or rationals. [1] https://en.wikipedia.org/wiki/Zero_divisor https://en.wikipedia.org/wiki/Zero_divisor