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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With