News for hackers. Stuff that matters.
Re:Python, unlike C, has the mod operator always return a po... by eesmith on Wednesday December 29, 2021 @04:41PM (#29730414)
K&R "The C Programming Language" (1978), p.37:

> The binary arithmetic operators are +, -, *, /, and the modulus operator %.

https://archive.org/details/TheCProgrammingLanguageFirstEdit...

At least, that was the C reference I used in college.

Re:Python, unlike C, has the mod operator always return a po... by klodolph on Wednesday December 29, 2021 @04:53PM (#29730560)
You'll only find that terminology in K&R. The C specification only uses "modulo" to refer to the behavior of unsigned numbers, and uses "remainder" to refer to the behavior of %. This is true of at least several versions of the C standard dating back to 1999, I don't have the C90 spec handy.
Re:Python, unlike C, has the mod operator always return a po... by eesmith on Wednesday December 29, 2021 @05:23PM (#29730865)
Indeed!

The draft version "X3.???-1988" at https://web.archive.org/web/20161223125339/http://flash-gord... (linked-to from Wikipedia) has:

> % modulus operator: 3.3.5

which seems off to a good start for K&R, but then also has:

> remainder operator, %, 3.3.5

and 3.3.5 says "the result of the % operator is the remainder" -- not a whiff of a modulus present there.

Thanks!

Re:Python, unlike C, has the mod operator always return a po... by somerando7 on Wednesday December 29, 2021 @05:11PM (#29730743)
People generally watch tutorials, read programming books, blogs, stackoverflow to learn programming, not the standard of a programming language.

You will find "modulus operator" to refer to % in C in all of the above (other than the standard).

Re:Python, unlike C, has the mod operator always return a po... by klodolph on Wednesday December 29, 2021 @05:35PM (#29731005)
That is what most people do... which is why those of us who write tutorials, write programming books, write blog posts, and write Stack Overflow answers have a responsibility to go to the source.

The more something gets repeated from person to person, the higher chance that somewhere in that chain, somebody screwed up. That's why people like professors, people who run YouTube programming channels, people who answer C questions on Stack Overflow, etc. should generally strive to cut down the number of links in the chain.

Re:Python, unlike C, has the mod operator always return a po... by xboxnolifes on Wednesday December 29, 2021 @05:36PM (#29731013)
The fact that tutorials teach incorrect knowledge does not make the knowledge correct.
Re:Python, unlike C, has the mod operator always return a po... by charcircuit on Wednesday December 29, 2021 @05:23PM (#29730864)
I think there is the same problem with the conditional operator "?" which a ton of resources name as the ternary operator.

edit: tertiary -> ternary

Re:Python, unlike C, has the mod operator always return a po... by eesmith on Wednesday December 29, 2021 @05:26PM (#29730898)
I know it as "the ternary operator" (used that way in K&R at https://archive.org/details/TheCProgrammingLanguageFirstEdit...), and until about 2 minutes ago had never heard it as "the conditional operator" - even though that's how the spec describes it!
Re:Python, unlike C, has the mod operator always return a po... by tialaramex on Wednesday December 29, 2021 @07:03PM (#29731779)
It gets named "the ternary operator" in many languages like C because they only have one ternary operator, it's always that one. In some (I think?) languages there are several ternary operators, and so for them it'd be like if we called C's XOR operator ^ "the binary operator".
Re:Python, unlike C, has the mod operator always return a po... by jholman on Thursday December 30, 2021 @06:39AM (#29735909)
Yeah. It's "the ternary operator" the way John Carmack is "the author of the tweet we're discussing". It's a correct description, but it's not his name.
Re:Python, unlike C, has the mod operator always return a po... by tialaramex on Thursday December 30, 2021 @08:00AM (#29736351)
Names are just whatever you call something. For inanimate objects the objects can't care what you call them, so at the very most you're privileging one person's opinion over another if you choose to call it the same thing. For ideas even more so. Ordinarily the most important purpose of the name is to ensure you're talking about the same thing.

Calling it "Ayers Rock" privileges some guy from the 19th century, calling it "Uluru" privileges a bunch of people who lived near it for longer than that, the rock itself has no opinion (and also doesn't believe anything about ritual magic, property law, gender based taboos, or the tourist industry)

Now, John Carmack is a little different because John is a person and it is rude to use names people don't like. But it isn't impossible, it's just rude. Calling the previous President of the United States of America "Tangerine Palpatine" works just fine - we both know who I meant. In fact it's a little weird that our culture has parents assign to their children full legal names, only kinda-sorta making it possible for them to choose for themselves later, the Culture's habit of allowing children to pick one of their names as part of growing up seems better.

Re:Python, unlike C, has the mod operator always return a po... by tialaramex on Wednesday December 29, 2021 @04:47PM (#29730477)
This is also exactly what the ANSI ("Second edition") K&R book I have in front of me says.

It goes on to say: "x % y produces the remainder when x is divided by y" and that "the sign of the result for % are machine-dependent for negative operands, as is the action taken on overflow or underflow".

Re:Python, unlike C, has the mod operator always return a po... by fanf2 on Wednesday December 29, 2021 @08:08PM (#29732276)
The language in the reference manual at the end of the book (p. 188) is much more specific. It says:

The binary operator / indicates division. When positive integers are divided, truncation is toward 0, but the form of truncation is machine-dependent if either operand is negative. On all machines covered by this manual, the remainder has the same sign as the dividend. It is always true that (a/b)*b + a%b is equal to a (if b is not 0). The binary % operator yields the remainder from division of the first operand by the second.

Re:Python, unlike C, has the mod operator always return a po... by jounker on Thursday December 30, 2021 @02:20AM (#29734497)
Interesting. If the equality must hold, then this means that a/b has different values on different architectures depending on whether a%b is positive or negative.