ABS

Elemental Intrinsic Function (Generic): Computes an absolute value.

Syntax

result=ABS(a)

a

(Input) Must be of type integer, real, or complex.

Results

The result has the same type and kind type parameter as a except if a is complex value, the result type is real. If a is an integer or real value, the value of the result is | a |; if a is a complex value (X, Y), the result is the real value SQRT (X**2 + Y**2).

Specific Name

Argument Type

Result Type

BABS

INTEGER(1)

INTEGER(1)

IIABS1

INTEGER(2)

INTEGER(2)

IABS 2

INTEGER(4)

INTEGER(4)

KIABS

INTEGER(8)

INTEGER(8)

ABS

REAL(4)

REAL(4)

DABS

REAL(8)

REAL(8)

QABS

REAL(16)

REAL(16)

CABS 3

COMPLEX(4)

REAL(4)

CDABS4

COMPLEX(8)

REAL(8)

CQABS

COMPLEX(16)

REAL(16)

1Or HABS.

2Or JIABS. For compatibility with older versions of Fortran, IABS can also be specified as a generic function.

3The setting of compiler options specifying real size can affect CABS.

4This function can also be specified as ZABS.

Example

ABS (-7.4) has the value 7.4.

ABS ((6.0, 8.0)) has the value 10.0.

The following ABS.F90 program calculates two square roots, retaining the sign:

REAL mag(2), sgn(2), result(2)

WRITE (*, '(A)') ' Enter two signed magnitudes: '

READ (*, *) mag

sgn = SIGN((/1.0, 1.0/), mag) ! transfer the signs to 1.0s

result = SQRT (ABS (mag))

! Restore the sign by multiplying by -1 or +1:

result = result * sgn

WRITE (*, *) result

END