MOD(3) - [NUMERIC] Remainder function
result = mod(a, p)
elemental type(TYPE(kind=KIND)) function mod(a,p)type(TYPE(kind=KIND)),intent(in) :: a type(TYPE(kind=KIND)),intent(in) :: p
o The result and arguments are all of the same type and kind. o The type may be any kind of real or integer.
MOD(3) computes the remainder of the division of A by P.
In mathematics, the remainder is the amount "left over" after performing some computation. In arithmetic, the remainder is the integer "left over" after dividing one integer by another to produce an integer quotient (integer division). In algebra of polynomials, the remainder is the polynomial "left over" after dividing one polynomial by another. The modulo operation is the operation that produces such a remainder when given a dividend and divisor.
o (remainder). (2022, October 10). In Wikipedia. https://en.wikipedia.org/wiki/Remainder
o A : The dividend o P : the divisor (not equal to zero).
The return value is the result of A - (INT(A/P) * P).
As can be seen by the formula the sign of P is canceled out. Therefore the returned value always has the sign of A.
Of course, the magnitude of the result will be less than the magnitude of P, as the result has been reduced by all multiples of P.
Sample program:
program demo_mod implicit noneResults:! basics print *, mod( -17, 3 ), modulo( -17, 3 ) print *, mod( 17, -3 ), modulo( 17, -3 ) print *, mod( 17, 3 ), modulo( 17, 3 ) print *, mod( -17, -3 ), modulo( -17, -3 )
print *, mod(-17.5, 5.2), modulo(-17.5, 5.2) print *, mod( 17.5,-5.2), modulo( 17.5,-5.2) print *, mod( 17.5, 5.2), modulo( 17.5, 5.2) print *, mod(-17.5,-5.2), modulo(-17.5,-5.2)
! with a divisor of 1 the fractional part is returned print *, mod(-17.5, 1.0), modulo(-17.5, 1.0) print *, mod( 17.5,-1.0), modulo( 17.5,-1.0) print *, mod( 17.5, 1.0), modulo( 17.5, 1.0) print *, mod(-17.5,-1.0), modulo(-17.5,-1.0)
end program demo_mod
> -2 1 > 2 -1 > 2 2 > -2 -2 > -1.900001 3.299999 > 1.900001 -3.299999 > 1.900001 1.900001 > -1.900001 -1.900001 > -0.5000000 0.5000000 > 0.5000000 -0.5000000 > 0.5000000 0.5000000 > -0.5000000 -0.5000000
FORTRAN 77
Fortran intrinsic descriptions (license: MIT) @urbanjost
o MODULO(3) - Modulo function o AINT(3) - truncate toward zero to a whole real number o INT(3) - truncate toward zero to a whole integer number o ANINT(3) - real nearest whole number o NINT(3) - integer nearest whole number
Nemo Release 3.1 | mod (3fortran) | November 02, 2024 |