Just another Perl shrine PerlMonks

### Re: Re: Re: 0 illegal modulus?

by mugwumpjism (Hermit)
 on Jun 15, 2001 at 00:20 UTC ( #88604=note: print w/replies, xml ) Need Help??

in reply to Re: Re: 0 illegal modulus?

"the intent of the modulus operator (x mod y) is to return the smallest number possible by repeatedly subtracting y from x."

Clearly provides the behaviour expected by the poster of this article. But who cares?

Any decision on what the function is or how it is defined is arbitrary, and as other people have said, if you are depending on nella's requested behaviour of mod, then you can always just use a ( \$y ? \$x % \$y : \$x ) construct.

Most people are familiar with division, hence a modulus is often explained as the remainer after dividing \$x by \$y. This obviously is not correct for negative values of \$x, unless you count a negative remainder as being a value subtracted from MAXINT (the modulus, in this case) in CS' two's complement tradition.

If you take a definition from a random hit for "modulo mathematical definition" taken from the WWW, for instance, you'll find the definition:

Two numbers a and b are said to be equal or congruent modulo N iff N|(a-b), i.e. iff their difference is exactly divisible by N. Usually (and on this page) a,b, are nonnegative and N a positive integer. We write a = b (mod N).

Note that the difference has to be divisible by N.

Another page I found expressed it like this:

number % sub means: map the number on the left-hand side onto the subset {0,sub) (0, zero inclusive, sub exclusive). If sub is negative, this should be (sub,0}, of course.

Note the words "sub exclusive". Why, then would you map the case where sub == 0 to {-infinity, infinity}? It doesn't follow the pattern.

Personally, I prefer to think of it in terms of what would the last digit be if expressing this number in base \$y? Of course, I think of negative numbers in twos complement form, and consider a negative modulus obfuscated programming (if I came across its use, I'd simply experiment to see what its behaviour was), so this works for me :-)

Interestingly, C on my platform gets it wrong for negative values of X and positive values of Y. I'm glad Perl doesn't.

ps. why would I want to read a book on number theory to understand why a basic operator behaves the way it does?

Replies are listed 'Best First'.
Re: 0 illegal modulus?
by jepri (Parson) on Jun 16, 2001 at 07:35 UTC
You say:

ps. why would I want to read a book on number theory to understand why a basic operator behaves the way it does?

The answer is "So you know what you are talking about". I admit I was too quick off the gun as well, however. After some further research I found that the modulo function was devised by Abel in the 1700's to allow him to study large and complicated groups with only pencil and paper.

Modulo is hardly a basic operator. It's just a basic operator for you because you've only ever used it in simple situations.

Computer implementations of functions are usually nasty bastardisations of real math functions. This is why the original poster mentioned Knuth, who did a lot of work involving math and computers.

Incidentally, 'proof by random web pages' is a poor way of proving that you are right.

____________________
Jeremy
I didn't believe in evil until I dated it.

(On reading a book to understand a simple principle)

You obviously don't get it. You can't expect someone who uses the % operator to have read a book on number theory first. Therefore it should behave intuitively and comply with what the commonly accepted understanding of the term is, so long as it is sensible and consistent. I think I have demonstrated in other posts that a modulus of 0 is neither sensible nor consistent, except with one contrived definition of modulus which I still think is bogus.

You say a modulus is defined one way; I say another. Who is to say who is right, or whether being "right" means anything? Let's end this rant and agree to disagree.

This is all so terribly familiar. I grew up programming (literally). Calculus at school was easy for me, because while the class was trying to understand what a function was, I was writing recursive ones. Simple simple simple. I taught myself math above my level so I could do tricks with my computer. I'm not boasting. The young monks here would have outshone me, but I was ahead of my classmates.

Math was so easy. But looking back every so often someone would point out something that didn't quite fit, I would call them stupid and sneer at them (like you have repeatedly in this thread) and they would go away.

It wasn't until I was taking advanced math at uni that I finally had it made completely clear to me what a twat I was being. The work was easy, except the lecturer kept focussing on stupid exceptions, like asymptotes, dividing functions in stupid situations, etc. The penny finally dropped that the real game was working with those exceptions, not the easy, 'intuitive' stuff.

Your educators have not been completely forthcoming. Pretty much everything you are told is a gross over-simplification or half-truth. The more honest amoung them will tell you that you get the lite version.

It would be possible for you to spend your whole life not understanding things better by belittling and attacking anyone who disagrees with you. This would be a shame, because it seems you are the kind of person who would get a kick out of the tougher stuff. Group theory might not be the best place to start, but it is relevent to many programs and techniques, so you would quickly begin to see it all over the place.

I would encourage you to start looking at things a bit more closely, with the thought in mind 'Why do people care so much about this?'. Often there's a reason, it's just hard to understand when you come at it from a different angle to most people who use it. Feel free to ask questions that are a bit 'off-topic', there's an amazing body of knowledge here.

____________________
Jeremy
I didn't believe in evil until I dated it.

Re^4: 0 illegal modulus?
by Anonymous Monk on Sep 25, 2012 at 22:00 UTC
You write: If you take a definition from a random hit for "modulo mathematical definition" taken from the WWW, for instance, you'll find the definition: Two numbers a and b are said to be equal or congruent modulo N iff N|(a-b), i.e. iff their difference is exactly divisible by N. Usually (and on this page) a,b, are nonnegative and N a positive integer. We write a = b (mod N). Note that the difference has to be divisible by N. Yes, and the difference between no two different integers is divisible by zero. Therefore all integers are in different congruence classes mod 0, which is another way of saying that x mod 0 = x, as the original poster claimed. Although of course it's a matter of definition whether (1) an integer is then congruent to itself mod 0 and (2) the mod "operator" is actually that closely related to the mod "arithmetic" that it returns the name of the congruence class under arithmetic mod that number. % is probably better called "remainder" than mod, anyhow, and again it makes sense for x % 0 to be x when you talk about remainders. No matter how often you subtract zero, x remains... (but that's the same argument others have made).

Create A New User
Node Status?
node history
Node Type: note [id://88604]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2017-07-20 13:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (303 votes). Check out past polls.