The important thing about the modulus operator is that the results are consistent with integer division. i.e., if you divide the modulus by the divisor and add it to the result of integer division you get the actual quotient. In Perl, you can test that like this:

((($a%$b)/$b)+(int($a/$b)))==($a/$b)

I think I got that right. Anyway, you get the idea. The point of the modulus is that it gives you the correct amount of "left over", the amount by which integer division is off.

Some languages have a special integer division operator (usually spelled div), but AFAIK Perl doesn't, so the int truncation is the way integer division is done, then.

So I suspect that Perl has defined the mod operator (spelled %) in such a way as to keep the above assertion intact given the way int truncates. So the real question you want to ask is about why integer division is not consistent between all languages. Why do some langauges truncate down and others always truncate toward zero?

And I think the answer to that probably comes down to hysterical raisins, but I don't know the details.


for(unpack("C*",'GGGG?GGGG?O__\?WccW?{GCw?Wcc{?Wcc~?Wcc{?~cc' .'W?')){$j=$_-63;++$a;for$p(0..7){$h[$p][$a]=$j%2;$j/=2}}for$ p(0..7){for$a(1..45){$_=($h[$p-1][$a])?'#':' ';print}print$/}

In reply to Re: Modulus Inconsistencies (Or Calling all Mathematicians) by jonadab
in thread Modulus Inconsistencies (Or Calling all Mathematicians) by RollyGuy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.