News for hackers. Stuff that matters.
Re:Python, unlike C, has the mod operator always return a po... by masklinn on Wednesday December 29, 2021 @05:09PM (#29730716)
> I kind of get what you are saying syntactically, but the semantics are weird to me as the C version is only defined for integers

Why? Mathematically, dividing two integers yields a real. If you want the result to be an integer, you need to define some rounding operation on the real result to produce an integer.

That is the source of the divergence between C and Python here, C rounds by truncation (towards 0), Python rounds by flooring (towards -inf).

Re:Python, unlike C, has the mod operator always return a po... by desio on Wednesday December 29, 2021 @05:29PM (#29730928)
> Mathematically, dividing two integers yields a real

not true. if you define 'divide' as inverse multiplication over the reals then that is closer to being true (zero is an integer, dividing an integer by zero doesn't yield a real). in that case every real can be expressed as n = a/b where a and b are integers and b != 0 then yes n is a real.

But 'divide' could just as well be defined along the lines of divisibility in number theory (the study of integers) where every integer can be expressed as n = q*b + r, where n,q,b,r are all integers and r >= 0 > b. then you could say that n divide b = q, with r as remainder, and n mod b = r. These are my preferred definition of mod and integer division.

Re:Python, unlike C, has the mod operator always return a po... by desio on Wednesday December 29, 2021 @05:30PM (#29730946)
sorry, rational, not real.
Re:Python, unlike C, has the mod operator always return a po... by jounker on Thursday December 30, 2021 @02:13AM (#29734467)
Depends on your definition of division. It you choose a definition which is closed (e.g. you’re working in number theory) then division of integers can only produce other integers.
Re:Python, unlike C, has the mod operator always return a po... by dragonwriter on Wednesday December 29, 2021 @05:22PM (#29730854)
> Mathematically, dividing two integers yields a real

Rational, more specifically.

Re:Python, unlike C, has the mod operator always return a po... by saurik on Wednesday December 29, 2021 @05:12PM (#29730758)
Oh, I'm dumb. Sorry. (I got the terms for quotient, dividend, and divisor mixed up.)
Re:Python, unlike C, has the mod operator always return a po... by masklinn on Wednesday December 29, 2021 @05:18PM (#29730827)
> Oh, I'm dumb.

We both know that's not true.

> Sorry. (I got the terms for quotient, dividend, and divisor mixed up.)

No big, brainfarts happen.

Re:Python, unlike C, has the mod operator always return a po... by DonHopkins on Wednesday December 29, 2021 @08:25PM (#29732422)
Just don't divide by zero. That's how you get black holes!
Re:Python, unlike C, has the mod operator always return a po... by LudwigNagasena on Wednesday December 29, 2021 @06:09PM (#29731327)
Mathematically, Euclidean division of two integers yields an integer. You don't need to define reals or rounding. Children learn how to do it before they learn about rational numbers.
Re:Python, unlike C, has the mod operator always return a po... by dotancohen on Thursday December 30, 2021 @12:58AM (#29734094)
I invite you to ask your child how he would share three cookies with you. I've been asking variations of that question to all three of my children since they could talk.

Of interesting mathematical consequence, each of my children converged on the same solution at different ages. Them: three cookies. Daddy: zero cookies.