Re: Negative Modulus

From: Mok-Kong Shen (mok-kong.shen_at_t-online.de)
Date: 03/15/04


Date: Mon, 15 Mar 2004 22:27:51 +0100


Douglas A. Gwyn wrote:

> Mok-Kong Shen wrote:
>
>>Both answers are correct. ...
>
>
> Actually the real issue is which of the infinite possibilities should be
> chosen for "the" answer. For positive modulus m the convention is to
> choose the answer in the range [0,m-1]. Since negative moduli are much
> less commonly used, any such convention for them is not as well
> established. There are probably some technical advantages in one choice
> rather than the other; I'm pretty sure Knuth discusses this in Vol. 1 of
> TAOCP, but my copy isn't at hand at the moment.

I have a question about C: For integers a and b, how is
a%b defined by the standard? (I unfortunately don't have
the document to check that myself.)

In Fortran one has, for example,
mod(8,5)=3, mod(-8,5)=-3, mod(8,-5)=3, mod(-8,-5)=-3.
It's interesting that Fortran has also a related function
'modulo' with
modulo(8,5)=3, modulo(-8,5)=2, modulo(8,-5)=-2, modulo(-8,-5)=-3.

I 'vaguely' remember that Pascal's convention is not identical
to at least one of the other PLs. If indeed it's true that
several major PLs differ in the conventions for mod, then it
would mean a certain potential pitfall for those people who
need to frequently switch PLs in their programming work.

M. K. Shen



Relevant Pages

  • Interfacing between Fortran and C - a little utility
    ... and C and I welcome the facilities that Fortran 2003 offers. ... It automatically determines what naming convention is used (from the ... FILE *outfile; ... "Unknown expected calling convention - ...
    (comp.lang.fortran)
  • Re: Fortran 90 modules suck for portability- why use them?
    ... I am new to Fortran but not to programming in general. ... library with codes compiled with another compiler. ... Sun's convention for generating linker names for contained ...
    (comp.lang.fortran)
  • Re: Loop Efficiency
    ... implies physical floating point measures or pixels. ... convention that dates all the way back to FORTRAN. ... obvious to use them as loop counters. ...
    (comp.lang.java.programmer)
  • Re: Loop Efficiency
    ... I wouldn't call it a convention. ... FORTRAN programmers grow out of once they realize that relying on IMPLICIT ...
    (comp.lang.java.programmer)
  • Re: abundance of irrationals!)
    ... Convention 1: A finite set is one which can be put into ... Infinite is defined as "not finite". ... > near zero, or at zero? ... but the values epsilon and x0 are variables. ...
    (sci.math)

Quantcast