| 1 |
# abs(x): Absolute Value |
| 2 |
|
| 3 |
Absolute value. |
| 4 |
|
| 5 |
## Declaration |
| 6 |
|
| 7 |
### Syntax |
| 8 |
|
| 9 |
```fortran |
| 10 |
retval = abs(x) |
| 11 |
elemental integer function iabs(x) |
| 12 |
elemental real(sp) function abs(x) |
| 13 |
``` |
| 14 |
|
| 15 |
### Arguments |
| 16 |
|
| 17 |
`x` the input value, can be integer, real, or complex. |
| 18 |
|
| 19 |
### Return values |
| 20 |
|
| 21 |
The return value is of type and kind same as of `x`. For complex input value, |
| 22 |
return value is real. |
| 23 |
|
| 24 |
## Description |
| 25 |
|
| 26 |
**abs(x)** calculates and returns absolute value of `x`. Result is calculated |
| 27 |
using mathematical formula: |
| 28 |
|
| 29 |
$|x|$ |
| 30 |
|
| 31 |
If `x` is complex, the result is calculated using mathematical formula: |
| 32 |
|
| 33 |
$\sqrt{(x^2 + y^2)}$ |
| 34 |
|
| 35 |
## Types |
| 36 |
|
| 37 |
Supported argument types are real and integer. |
| 38 |
|
| 39 |
```fortran |
| 40 |
interface mod |
| 41 |
module procedure iabs, i8abs, i16abs, i64abs, sabs, dabs, cabs, zabs |
| 42 |
end interface |
| 43 |
|
| 44 |
contains |
| 45 |
|
| 46 |
elemental integer(i16) function i16abs(x) |
| 47 |
integer(i16), intent(in) :: x |
| 48 |
end function |
| 49 |
|
| 50 |
elemental integer(i8) function i8abs(x) |
| 51 |
integer(i8), intent(in) :: x |
| 52 |
end function |
| 53 |
|
| 54 |
elemental integer function iabs(x) |
| 55 |
integer, intent(in) :: x |
| 56 |
end function |
| 57 |
|
| 58 |
elemental integer function i64abs(x) |
| 59 |
integer(i64), intent(in) :: x |
| 60 |
end function |
| 61 |
|
| 62 |
elemental real(sp) function sabs(x) |
| 63 |
real(sp), intent(in) :: x |
| 64 |
end function |
| 65 |
|
| 66 |
elemental real(dp) function dabs(x) |
| 67 |
real(dp), intent(in) :: x |
| 68 |
end function |
| 69 |
|
| 70 |
elemental real(sp) function cabs(x) |
| 71 |
complex(sp), intent(in) :: x |
| 72 |
end function |
| 73 |
|
| 74 |
elemental real(dp) function zabs(x) |
| 75 |
complex(dp), intent(in) :: x |
| 76 |
end function |
| 77 |
``` |
| 78 |
|
| 79 |
## Examples |
| 80 |
|
| 81 |
```fortran |
| 82 |
program intrinsics_abs |
| 83 |
implicit none |
| 84 |
print *, abs(1.5) |
| 85 |
print *, abs(-1.5) |
| 86 |
end program |
| 87 |
``` |
| 88 |
|
| 89 |
**Result:** |
| 90 |
|
| 91 |
``` |
| 92 |
1.5 |
| 93 |
1.5 |
| 94 |
``` |
| 95 |
|
| 96 |
## See Also |
| 97 |
|
| 98 |
[ceiling](ceiling.md), [floor](floor.md). |