difference between mod and modulo - Fortran

This is a discussion on difference between mod and modulo - Fortran ; Hi, what is the difference between the MOD and the MODULO functions in Fortran 90? According to the book Fortran 90-95 Explained, mod(a,p) returns a-int(a/p)*p and modulo(a,p) returns a-floor(a/p)*p if a and p are real, or a-floor(a//p)*p if a and ...

+ Reply to Thread
Results 1 to 6 of 6

difference between mod and modulo

  1. Default difference between mod and modulo

    Hi,

    what is the difference between the MOD and the MODULO functions in
    Fortran 90? According to the book Fortran 90-95 Explained,
    mod(a,p) returns a-int(a/p)*p and
    modulo(a,p) returns a-floor(a/p)*p if a and p are real,
    or a-floor(a//p)*p if a and p are integers,
    where // is "ordinary mathematical division".

    I don't see what the difference is. In my tests, both functions always
    return the same value.

    Thanks,
    Michael

  2. Default Re: difference between mod and modulo

    Michael Goerz wrote, on 07/14/2008 06:51 PM:
    > Hi,
    >
    > what is the difference between the MOD and the MODULO functions in
    > Fortran 90? According to the book Fortran 90-95 Explained,
    > mod(a,p) returns a-int(a/p)*p and
    > modulo(a,p) returns a-floor(a/p)*p if a and p are real,
    > or a-floor(a//p)*p if a and p are integers,
    > where // is "ordinary mathematical division".
    >
    > I don't see what the difference is. In my tests, both functions always
    > return the same value.
    >
    > Thanks,
    > Michael

    I think I found it... mod seems to respect the sign, whereas modulo does
    not. That is,
    mod(-5.0, 2.0) = -1.0
    modulo(-5.0, 2.0) = 1.0

    Michael

  3. Default Re: difference between mod and modulo

    Michael Goerz <newsgroup812sfie@8439.e4ward.com> wrote:

    > Hi,
    >
    > what is the difference between the MOD and the MODULO functions in
    > Fortran 90? According to the book Fortran 90-95 Explained,
    > mod(a,p) returns a-int(a/p)*p and
    > modulo(a,p) returns a-floor(a/p)*p if a and p are real,
    > or a-floor(a//p)*p if a and p are integers,
    > where // is "ordinary mathematical division".
    >
    > I don't see what the difference is. In my tests, both functions always
    > return the same value.


    I bet your tests didn't include negative numbers. That's where the
    difference is. MODULO is the mathematical function I'm more familliar
    with. It always returns a value on the range 0 to p, just like, for
    example, a clock. MOD can also return values between 0 and -p, which can
    be a bit of a problem if you forget about that case and were counting on
    mod to give a result between 0 and p (I've seen that bug happen).

    Modulo(-10,60) gives 50

    Mod(-10,60) gives -10.

    --
    Richard Maine | Good judgement comes from experience;
    email: last name at domain . net | experience comes from bad judgement.
    domain: summertriangle | -- Mark Twain

  4. Default Re: difference between mod and modulo

    "Michael Goerz" <newsgroup812sfie@8439.e4ward.com> wrote in message
    news:4udsk5-1oq.ln1@ID-262785.user.uni-berlin.de...

    > I think I found it... mod seems to respect the sign, whereas modulo does
    > not. That is,
    > mod(-5.0, 2.0) = -1.0
    > modulo(-5.0, 2.0) = 1.0


    The way I think about it is that the sign of the result of mod(a,p)
    follows the sign of a whereas the sign of the result of modulo(a,p)
    follows the sign of p. Typically I find the latter is the action I
    want:

    C:\gfortran\clf\mod_test>type mod_test.f90
    program mod_test
    write(*,*) 'mod(0,100) = ', mod(0,100)
    write(*,*) 'mod(30,100) = ', mod(30,100)
    write(*,*) 'mod(100,100) = ', mod(100,100)
    write(*,*) 'mod(-30,100) = ', mod(-30,100)
    write(*,*) 'mod(-100,100) = ', mod(-100,100)
    write(*,*) 'mod(0,-100) = ', mod(0,-100)
    write(*,*) 'mod(30,-100) = ', mod(30,-100)
    write(*,*) 'mod(100,-100) = ', mod(100,-100)
    write(*,*) 'mod(-30,-100) = ', mod(-30,-100)
    write(*,*) 'mod(-100,-100) = ', mod(-100,-100)
    write(*,*) 'modulo(0,100) = ', modulo(0,100)
    write(*,*) 'modulo(30,100) = ', modulo(30,100)
    write(*,*) 'modulo(100,100) = ', modulo(100,100)
    write(*,*) 'modulo(-30,100) = ', modulo(-30,100)
    write(*,*) 'modulo(-100,100) = ', modulo(-100,100)
    write(*,*) 'modulo(0,-100) = ', modulo(0,-100)
    write(*,*) 'modulo(30,-100) = ', modulo(30,-100)
    write(*,*) 'modulo(100,-100) = ', modulo(100,-100)
    write(*,*) 'modulo(-30,-100) = ', modulo(-30,-100)
    write(*,*) 'modulo(-100,-100) = ', modulo(-100,-100)
    end program mod_test

    C:\gfortran\clf\mod_test>gfortran mod_test.f90 -omod_test

    C:\gfortran\clf\mod_test>mod_test
    mod(0,100) = 0
    mod(30,100) = 30
    mod(100,100) = 0
    mod(-30,100) = -30
    mod(-100,100) = 0
    mod(0,-100) = 0
    mod(30,-100) = 30
    mod(100,-100) = 0
    mod(-30,-100) = -30
    mod(-100,-100) = 0
    modulo(0,100) = 0
    modulo(30,100) = 30
    modulo(100,100) = 0
    modulo(-30,100) = 70
    modulo(-100,100) = 0
    modulo(0,-100) = 0
    modulo(30,-100) = -70
    modulo(100,-100) = 0
    modulo(-30,-100) = -30
    modulo(-100,-100) = 0

    --
    write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
    6.0134700243160014d-154/),(/'x'/)); end



  5. Default Re: difference between mod and modulo

    Michael Goerz wrote:
    > Michael Goerz wrote, on 07/14/2008 06:51 PM:

    ....
    >> what is the difference between the MOD and the MODULO functions in
    >> Fortran 90? According to the book Fortran 90-95 Explained,
    >> mod(a,p) returns a-int(a/p)*p and
    >> modulo(a,p) returns a-floor(a/p)*p if a and p are real,
    >> or a-floor(a//p)*p if a and p are integers,
    >> where // is "ordinary mathematical division".

    ....
    > I think I found it... mod seems to respect the sign, whereas modulo
    > does not. That is,
    > mod(-5.0, 2.0) = -1.0
    > modulo(-5.0, 2.0) = 1.0


    In both functions the magnitude of the result is between zero
    and the magnitude of the denominator. For MOD the sign of
    the result follows the sign of the numerator. For MODULO the
    sign of the result follows the sign of the denominator.

    If you have a positive modulus, the definition of MODULO
    is the same as the usual mathematical usage for modular
    arithmetic. MODULO gives you the non-negative base
    elements of the modular equivalence classes for the
    given modulus.

    On the other hand, MOD gives you the remainder function
    for the Fortran definition of integer division - since Fortran's
    divide truncates toward zero. It's a shame that there's this
    difference. It would have been simpler to have only one
    remaindering function. But, it turns out that truncating
    divide is a lot eassier to implement in hardware, so that's
    what the early computer engineers did. And, truncating
    divide has some advantages - and most programmers
    have come to expect it.

    --
    J. Giles

    "I conclude that there are two ways of constructing a software
    design: One way is to make it so simple that there are obviously
    no deficiencies and the other way is to make it so complicated
    that there are no obvious deficiencies." -- C. A. R. Hoare

    "Simplicity is prerequisite for reliability" -- E. W. Dijkstra



  6. Default Re: difference between mod and modulo

    On Mon, 14 Jul 2008 20:01:35 GMT, James Giles posted:

    > Michael Goerz wrote:
    >> Michael Goerz wrote, on 07/14/2008 06:51 PM:

    > ...
    >>> what is the difference between the MOD and the MODULO functions in
    >>> Fortran 90? According to the book Fortran 90-95 Explained,
    >>> mod(a,p) returns a-int(a/p)*p and
    >>> modulo(a,p) returns a-floor(a/p)*p if a and p are real,
    >>> or a-floor(a//p)*p if a and p are integers,
    >>> where // is "ordinary mathematical division".

    > ...
    >> I think I found it... mod seems to respect the sign, whereas modulo
    >> does not. That is,
    >> mod(-5.0, 2.0) = -1.0
    >> modulo(-5.0, 2.0) = 1.0

    >
    > In both functions the magnitude of the result is between zero
    > and the magnitude of the denominator. For MOD the sign of
    > the result follows the sign of the numerator. For MODULO the
    > sign of the result follows the sign of the denominator.
    >
    > If you have a positive modulus, the definition of MODULO
    > is the same as the usual mathematical usage for modular
    > arithmetic. MODULO gives you the non-negative base
    > elements of the modular equivalence classes for the
    > given modulus.
    >
    > On the other hand, MOD gives you the remainder function
    > for the Fortran definition of integer division - since Fortran's
    > divide truncates toward zero. It's a shame that there's this
    > difference. It would have been simpler to have only one
    > remaindering function. But, it turns out that truncating
    > divide is a lot eassier to implement in hardware, so that's
    > what the early computer engineers did. And, truncating
    > divide has some advantages - and most programmers
    > have come to expect it.


    Curious to hear you talk of a denominator here, in particular when I know
    that your background in algebra is strong.
    --
    Immorality: the morality of those who are having a better time.
    H. L. Mencken

+ Reply to Thread