Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Because it's not computing congruence

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

in reply to 0 illegal modulus?

In Mathematics terms, for two numbers A,B to be congruent mod N, then A - B must be an exact multiple of N. So in other words, it is a three argument comparison operator that returns a true/false value, rather than a two argument function like Perl's mod that returns a number between 0 and N-1 (or N+1 and 0 if N is negative).

But Perl's (and other computer languages') mod operator is based on mapping numbers to what mathematicians call fields; in this case a "wrap-around" set of numbers from 0 to N-1 (or from N+1 to 0 if N is negative). In this context, a field with length 0 doesn't make much sense, so is by default an exception.

There are good reasons for this; for instance, much code assumes that if A = B mod N, then abs(A) < abs(N). (abs() being the absolute operator). Knuth's decision that "x mod 0 is defined to be x" may make sense to one school of thought, but to another it's bogus.

If you don't like the behaviour, use a ( $N ? $A % $N : $A ) construct, or see the section in Perl 6 on use limits, which allows you to define the behaviour of this special case.

Update: OK, IANAM and apparently it's only a field when N is prime. But hopefully you can understand what I mean :)

Replies are listed 'Best First'.
Re: Because it's not computing congruence
by Tiefling (Monk) on Jun 15, 2001 at 14:33 UTC

    The bogosity in Knuth's argument, for any mathematicians who are interested, is that it is - from the point of view of at least one valid way of working with moduli - a patch for something that wasn't broken, and the Wrong Thing. From the point of view of the theory of limits, it's probably the Right Answer, but that doesn't (necessarily) mean that patching the modulus system to work that way is necessary or desirable.

    I personally think that Perl's mod, and other computer languages', should behave the way Knuth's definition suggests, simply because I'd rather my functions gave answers than not. But I admit that it does, in various ways, break the consistency of the definition to do so.

    Perl 6 is going to have special features for this kind of 'filled in' definition. Can someone enlarge for this relative newcomer on what use limits will do?

    Tiefling (who is imaginary, but appears real at the limit)

    -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GAT d++ s:- a-- C++ UL P++ L++(+) E? W+(++) N+ o? K w+(--) !O M- V? PS+ PE- Y PGP- t+ 5 X+ R+++ tv- b+++ DI++++ D+ G+ e++ h!(-) y +? ------END GEEK CODE BLOCK------

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://88738]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2022-05-17 05:04 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (65 votes). Check out past polls.