5 ms·
It should be noted that in C89, both behaviors of division and modulo are allowed and it's implementation-defined whether division results are truncated or roun
by nwellnhof 3y ago
It should be noted that in C89, both behaviors of division and modulo are allowed and it's implementation-defined whether division results are truncated or rounded towards negative infinity. This has changed in C99 which only allows truncation towards zero.
- kragen 3y agomentioned in the comments on the post, which merit reading in this case i haven't read the rationale, but presumably the committee did this because it's what virtually all cpus do (because it's what fortran does), so it's the only thing that can be implemented efficiently on virtually any cpu with a division instruction, and so virtually all c implementations did it, and standardizing the behavior of virtually all c implementations is better than leaving it implementation-defined or standardizing a behavior that conflicts with virtually all existing implementations and can't be implemented efficiently on most high-end hardware
- DonHopkins 3y agoWho would have ever thought that division could be so ... divisive?
- kragen 3y agoas sophie w. arm said, 'i have a multiplier, not a divider'
- DonHopkins 3y agoAt the RISC of sounding short on instructions, do you mean Sophie Wilson's ARM? ;) https://en.wikipedia.org/wiki/Sophie_Wilson https://en.wikipedia.org/wiki/Sophie_Wilson
- Findecanor 3y agoOne thing that is still "implementation-defined" in C and C++ is the result of a right shift of a negative integer. On pretty much all platforms, the >> operator shifts in the sign bit and does not round the result — which makes it equivalent to flooring division by a power of two. It is consistent with the division operator only when the left-hand value is positive.
- kazinator 3y agoHowever, the oposite shift is undefined if a 1 goes into the sign bit. More precisely, regarding E1 << E2, it is written (in the April 2023 ISO C draft): "If E1 has a signed type and nonnegative value, and E1 × 2ᴱ² is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined." Thus if E1 is negative, or if the result overflows, UB.
- rcfox 3y agoDidn't they specify two's complement signed integers somewhat recently? What's the rationale for leaving these behaviours undefined?
- kazinator 3y agoBecause the shift operations are arithmetically defined, and the situations that are undefined correspond to overflow. v << 1 means the same thing as v * 2 and is undefined if v * 2 is undefined.
- Findecanor 3y agoOn some CPUs architectures, the operand size for some instructions could be larger than an `int`, in which case the upper part of a CPU register would become invalid on overflow instead of containing an extension of the sign bit. There are also CPUs that do "saturating arithmetic" where overflow results in INT_MAX instead of wrapping.
- Karellen 3y agoI've always considered it a mistake to refer to "%" as a "modulo" operator in C because of its ability to return negative numbers. That's not how modulo arithmetic works. C's "%" is a remainder operator.
- deleted 3y ago[deleted]
- pwdisswordfishc 3y agoExactly. Modulo is not an operator, but a term for an equivalence class of differing by a multiple of a number fixed in advance.
- mathgradthrow 3y agoThat's an operator. A map to the quotient.
- Karellen 3y ago> Modulo is not an operator, "operator" is being used as a term of art in programming language design, to describe a symbol used to indicate that an operation should be performed on one or more operands. https://en.wikipedia.org/wiki/Operator_(computer_programming) https://en.wikipedia.org/wiki/Operator_(computer_programming... So the "%" symbol is an operator in C and Python, and it is standard usage to describe the operator that performs a mathematical function "X" as the "X operator".
- marko_oktabyr 3y agoAs sibling comments have mentioned, "operator" here is being used in the programming language sense of the word, not the mathematical sense. Using the name "mod" [as in mod(a,n)] or "modulo" for the mapping that takes integers to a particular representative of its equivalence class in Z/nZ ("integers modulo n") doesn't seem like an unreasonable choice to me.
- kazinator 3y ago(mod N) is an operator which indicates that the arithmetic in the preceding formula or equation is understood as taking place in a modulo N congruence. A triple equal sign is usually used for modular equations. That triple equal sign, together with the (mod N) out to the right, constitute an operator. -1 ≡ 9 (mod 10) Since it is written as part of the syntax of a formula or equation, and modifies its semantics, it is an operator. Just like d/dx, sigma notation and whatever not.