| 1 |
# mod(x, y): Modulus |
| 2 |
|
| 3 |
Modulus or remainder function. |
| 4 |
|
| 5 |
## Declaration |
| 6 |
|
| 7 |
### Syntax |
| 8 |
|
| 9 |
```fortran |
| 10 |
retval = mod(x, y) |
| 11 |
elemental integer function imod(x, y) |
| 12 |
elemental real(sp) function mod(x, y) |
| 13 |
``` |
| 14 |
|
| 15 |
### Arguments |
| 16 |
|
| 17 |
`x` the input value, can be integer or real. |
| 18 |
|
| 19 |
`y` second input parameter should be same type and kind as `x` and not equal to |
| 20 |
0. |
| 21 |
|
| 22 |
### Return values |
| 23 |
|
| 24 |
The return value is of type and kind same as of `x`. The result is calculated |
| 25 |
using mathematical modulo or remainder as: |
| 26 |
|
| 27 |
`x - INT (x / y) * y`. |
| 28 |
|
| 29 |
If `y` is 0, the result is undefined like `any integer value / 0`. |
| 30 |
|
| 31 |
## Description |
| 32 |
|
| 33 |
**mod(x, y)** calculates and returns modulo or remainder when `x` is divided |
| 34 |
by `y`. The result is calculated using |
| 35 |
|
| 36 |
`x - INT(x / y) * y` |
| 37 |
|
| 38 |
i.e., result is remainder when first argument is mathematically divided by |
| 39 |
second argument. |
| 40 |
|
| 41 |
The return value should be less than `y`. |
| 42 |
|
| 43 |
## Types |
| 44 |
|
| 45 |
Supported argument types are real and integer. |
| 46 |
|
| 47 |
```fortran |
| 48 |
interface mod |
| 49 |
module procedure i8mod, i16mod, imod, i64mod, smod, dmod |
| 50 |
end interface |
| 51 |
|
| 52 |
contains |
| 53 |
|
| 54 |
elemental integer(i16) function i16mod(x, y) |
| 55 |
integer(i16), intent(in) :: x, y |
| 56 |
end function |
| 57 |
|
| 58 |
elemental integer(i8) function i8mod(x, y) |
| 59 |
integer(i8), intent(in) :: x, y |
| 60 |
end function |
| 61 |
|
| 62 |
elemental integer function imod(x, y) |
| 63 |
integer, intent(in) :: x, y |
| 64 |
end function |
| 65 |
|
| 66 |
elemental integer function i64mod(x, y) |
| 67 |
integer(i64), intent(in) :: x, y |
| 68 |
end function |
| 69 |
|
| 70 |
elemental real(sp) function smod(x, y) |
| 71 |
real(sp), intent(in) :: x, y |
| 72 |
end function |
| 73 |
|
| 74 |
elemental real(dp) function dmod(x, y) |
| 75 |
real(dp), intent(in) :: x, y |
| 76 |
end function |
| 77 |
|
| 78 |
``` |
| 79 |
|
| 80 |
## Examples |
| 81 |
|
| 82 |
```fortran |
| 83 |
program intrinsics_mod |
| 84 |
implicit none |
| 85 |
print *, mod(-5, 3) |
| 86 |
end program |
| 87 |
``` |
| 88 |
|
| 89 |
**Result:** |
| 90 |
|
| 91 |
``` |
| 92 |
-2 |
| 93 |
``` |
| 94 |
|
| 95 |
## See Also |
| 96 |
|
| 97 |
[ceiling](ceiling.md), [floor](floor.md). |