Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran MOD and MODULO giving same results

Compiling with gfortran --version == GNU Fortran (Homebrew GCC 8.2.0) 8.2.0

When I write the following test code I get the functions MOD and MODULO giving the same results. However, as I understand it from several sources, MOD is supposed to give the remainder, and MODULO is supposed to give the integer quotient.

program test
    implicit none

    print *, mod(17,3)
    print *, mod(17.5,5.5)
    print *, mod(17.5d0,5.5)
    print *, mod(17.5,5.5d0)

    print *, modulo(17,3)
    print *, modulo(17.5,5.5)
    print *, modulo(17.5d0,5.5)
    print *, modulo(17.5,5.5d0)
end program test

Prints out:

2

1.00000000

1.0000000000000000

1.0000000000000000

2

1.00000000

1.0000000000000000

1.0000000000000000

The results are all the remainders, which is what MOD is supposed to do, but for MODULO isn't it supposed to print out the modulus, in this case 5 and then three 3's to varying precision?

like image 960
Xatticus00 Avatar asked Nov 28 '18 21:11

Xatticus00


1 Answers

You might be misunderstanding the purpose of these functions. They are supposed to give you the same answer, namely the remainder after division, when both arguments are positive. But consider the situation with negative arguments:

print *, mod( 17,  3)
print *, mod( 17., 3.)
print *, mod(-17,  3)
print *, mod(-17., 3.)
print *, modulo(-17,  3)
print *, modulo(-17., 3.)

which will return

2
2.000000
-2
-2.000000
1
1.000000

so the result of MODULO(A,P) will be positive for positive P and in fact must have the same sign as P. MODULO works differently depending on whether A and P are integers or real numbers. See the documentation for the exact definitions

MOD MODULO

like image 155
Vladimir F Героям слава Avatar answered Oct 23 '22 22:10

Vladimir F Героям слава