This module provides small, elemental numeric helpers that are commonly
needed but missing from the Fortran intrinsics: clamp, is_in_range
and is_close. All procedures are elemental, so they apply equally to
scalars and to arrays of any rank/shape.
Constrain a value to the inclusive range [lo, hi].
Returns lo if x < lo, hi if x > hi, otherwise x. Supported
types are: integer(int32), integer(int64), real(sp), real(dp).
Usage: y = clamp(x, lo, hi)
Clamp an integer(int32) value to [lo, hi].
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=int32), | intent(in) | :: | x | |||
| integer(kind=int32), | intent(in) | :: | lo | |||
| integer(kind=int32), | intent(in) | :: | hi |
Clamp an integer(int64) value to [lo, hi].
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=int64), | intent(in) | :: | x | |||
| integer(kind=int64), | intent(in) | :: | lo | |||
| integer(kind=int64), | intent(in) | :: | hi |
Clamp a real(sp) value to [lo, hi].
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=sp), | intent(in) | :: | x | |||
| real(kind=sp), | intent(in) | :: | lo | |||
| real(kind=sp), | intent(in) | :: | hi |
Clamp a real(dp) value to [lo, hi].
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | x | |||
| real(kind=dp), | intent(in) | :: | lo | |||
| real(kind=dp), | intent(in) | :: | hi |
Test two floating-point values for approximate equality.
Returns .true. when
abs(a - b) <= max(rtol*max(abs(a), abs(b)), atol).
The relative tolerance rtol defaults to sqrt(epsilon) for the
working kind and the absolute tolerance atol defaults to 0; both
are optional. Supported kinds are real(sp) and real(dp).
Usage: flag = is_close(a, b [, rtol] [, atol])
Approximate equality for real(sp) values.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=sp), | intent(in) | :: | a | |||
| real(kind=sp), | intent(in) | :: | b | |||
| real(kind=sp), | intent(in), | optional | :: | rtol | ||
| real(kind=sp), | intent(in), | optional | :: | atol |
Approximate equality for real(dp) values.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | a | |||
| real(kind=dp), | intent(in) | :: | b | |||
| real(kind=dp), | intent(in), | optional | :: | rtol | ||
| real(kind=dp), | intent(in), | optional | :: | atol |
Test whether a value lies within the inclusive range [lo, hi].
Returns .true. when lo <= x .and. x <= hi. Supported types are:
integer(int32), integer(int64), real(sp), real(dp).
Usage: flag = is_in_range(x, lo, hi)
Inclusive range test for integer(int32).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=int32), | intent(in) | :: | x | |||
| integer(kind=int32), | intent(in) | :: | lo | |||
| integer(kind=int32), | intent(in) | :: | hi |
Inclusive range test for integer(int64).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=int64), | intent(in) | :: | x | |||
| integer(kind=int64), | intent(in) | :: | lo | |||
| integer(kind=int64), | intent(in) | :: | hi |
Inclusive range test for real(sp).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=sp), | intent(in) | :: | x | |||
| real(kind=sp), | intent(in) | :: | lo | |||
| real(kind=sp), | intent(in) | :: | hi |
Inclusive range test for real(dp).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | x | |||
| real(kind=dp), | intent(in) | :: | lo | |||
| real(kind=dp), | intent(in) | :: | hi |
Clamp a real(dp) value to [lo, hi].
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | x | |||
| real(kind=dp), | intent(in) | :: | lo | |||
| real(kind=dp), | intent(in) | :: | hi |
Clamp an integer(int32) value to [lo, hi].
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=int32), | intent(in) | :: | x | |||
| integer(kind=int32), | intent(in) | :: | lo | |||
| integer(kind=int32), | intent(in) | :: | hi |
Clamp an integer(int64) value to [lo, hi].
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=int64), | intent(in) | :: | x | |||
| integer(kind=int64), | intent(in) | :: | lo | |||
| integer(kind=int64), | intent(in) | :: | hi |
Clamp a real(sp) value to [lo, hi].
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=sp), | intent(in) | :: | x | |||
| real(kind=sp), | intent(in) | :: | lo | |||
| real(kind=sp), | intent(in) | :: | hi |
Approximate equality for real(dp) values.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | a | |||
| real(kind=dp), | intent(in) | :: | b | |||
| real(kind=dp), | intent(in), | optional | :: | rtol | ||
| real(kind=dp), | intent(in), | optional | :: | atol |
Approximate equality for real(sp) values.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=sp), | intent(in) | :: | a | |||
| real(kind=sp), | intent(in) | :: | b | |||
| real(kind=sp), | intent(in), | optional | :: | rtol | ||
| real(kind=sp), | intent(in), | optional | :: | atol |
Inclusive range test for real(dp).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | x | |||
| real(kind=dp), | intent(in) | :: | lo | |||
| real(kind=dp), | intent(in) | :: | hi |
Inclusive range test for integer(int32).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=int32), | intent(in) | :: | x | |||
| integer(kind=int32), | intent(in) | :: | lo | |||
| integer(kind=int32), | intent(in) | :: | hi |
Inclusive range test for integer(int64).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=int64), | intent(in) | :: | x | |||
| integer(kind=int64), | intent(in) | :: | lo | |||
| integer(kind=int64), | intent(in) | :: | hi |
Inclusive range test for real(sp).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=sp), | intent(in) | :: | x | |||
| real(kind=sp), | intent(in) | :: | lo | |||
| real(kind=sp), | intent(in) | :: | hi |