pic_math Module

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.


Uses

  • module~~pic_math~~UsesGraph module~pic_math pic_math module~pic_types pic_types module~pic_math->module~pic_types iso_c_binding iso_c_binding module~pic_types->iso_c_binding iso_fortran_env iso_fortran_env module~pic_types->iso_fortran_env

Interfaces

public interface clamp

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)

  • private elemental function clamp_int32(x, lo, hi) result(y)

    Clamp an integer(int32) value to [lo, hi].

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=int32), intent(in) :: x
    integer(kind=int32), intent(in) :: lo
    integer(kind=int32), intent(in) :: hi

    Return Value integer(kind=int32)

  • private elemental function clamp_int64(x, lo, hi) result(y)

    Clamp an integer(int64) value to [lo, hi].

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=int64), intent(in) :: x
    integer(kind=int64), intent(in) :: lo
    integer(kind=int64), intent(in) :: hi

    Return Value integer(kind=int64)

  • private elemental function clamp_sp(x, lo, hi) result(y)

    Clamp a real(sp) value to [lo, hi].

    Arguments

    Type IntentOptional Attributes Name
    real(kind=sp), intent(in) :: x
    real(kind=sp), intent(in) :: lo
    real(kind=sp), intent(in) :: hi

    Return Value real(kind=sp)

  • private elemental function clamp_dp(x, lo, hi) result(y)

    Clamp a real(dp) value to [lo, hi].

    Arguments

    Type IntentOptional Attributes Name
    real(kind=dp), intent(in) :: x
    real(kind=dp), intent(in) :: lo
    real(kind=dp), intent(in) :: hi

    Return Value real(kind=dp)

public interface is_close

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])

  • private elemental function is_close_sp(a, b, rtol, atol) result(flag)

    Approximate equality for real(sp) values.

    Arguments

    Type IntentOptional 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

    Return Value logical

  • private elemental function is_close_dp(a, b, rtol, atol) result(flag)

    Approximate equality for real(dp) values.

    Arguments

    Type IntentOptional 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

    Return Value logical

public interface is_in_range

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)

  • private elemental function is_in_range_int32(x, lo, hi) result(flag)

    Inclusive range test for integer(int32).

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=int32), intent(in) :: x
    integer(kind=int32), intent(in) :: lo
    integer(kind=int32), intent(in) :: hi

    Return Value logical

  • private elemental function is_in_range_int64(x, lo, hi) result(flag)

    Inclusive range test for integer(int64).

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=int64), intent(in) :: x
    integer(kind=int64), intent(in) :: lo
    integer(kind=int64), intent(in) :: hi

    Return Value logical

  • private elemental function is_in_range_sp(x, lo, hi) result(flag)

    Inclusive range test for real(sp).

    Arguments

    Type IntentOptional Attributes Name
    real(kind=sp), intent(in) :: x
    real(kind=sp), intent(in) :: lo
    real(kind=sp), intent(in) :: hi

    Return Value logical

  • private elemental function is_in_range_dp(x, lo, hi) result(flag)

    Inclusive range test for real(dp).

    Arguments

    Type IntentOptional Attributes Name
    real(kind=dp), intent(in) :: x
    real(kind=dp), intent(in) :: lo
    real(kind=dp), intent(in) :: hi

    Return Value logical


Functions

private elemental function clamp_dp(x, lo, hi) result(y)

Clamp a real(dp) value to [lo, hi].

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: x
real(kind=dp), intent(in) :: lo
real(kind=dp), intent(in) :: hi

Return Value real(kind=dp)

private elemental function clamp_int32(x, lo, hi) result(y)

Clamp an integer(int32) value to [lo, hi].

Arguments

Type IntentOptional Attributes Name
integer(kind=int32), intent(in) :: x
integer(kind=int32), intent(in) :: lo
integer(kind=int32), intent(in) :: hi

Return Value integer(kind=int32)

private elemental function clamp_int64(x, lo, hi) result(y)

Clamp an integer(int64) value to [lo, hi].

Arguments

Type IntentOptional Attributes Name
integer(kind=int64), intent(in) :: x
integer(kind=int64), intent(in) :: lo
integer(kind=int64), intent(in) :: hi

Return Value integer(kind=int64)

private elemental function clamp_sp(x, lo, hi) result(y)

Clamp a real(sp) value to [lo, hi].

Arguments

Type IntentOptional Attributes Name
real(kind=sp), intent(in) :: x
real(kind=sp), intent(in) :: lo
real(kind=sp), intent(in) :: hi

Return Value real(kind=sp)

private elemental function is_close_dp(a, b, rtol, atol) result(flag)

Approximate equality for real(dp) values.

Arguments

Type IntentOptional 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

Return Value logical

private elemental function is_close_sp(a, b, rtol, atol) result(flag)

Approximate equality for real(sp) values.

Arguments

Type IntentOptional 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

Return Value logical

private elemental function is_in_range_dp(x, lo, hi) result(flag)

Inclusive range test for real(dp).

Arguments

Type IntentOptional Attributes Name
real(kind=dp), intent(in) :: x
real(kind=dp), intent(in) :: lo
real(kind=dp), intent(in) :: hi

Return Value logical

private elemental function is_in_range_int32(x, lo, hi) result(flag)

Inclusive range test for integer(int32).

Arguments

Type IntentOptional Attributes Name
integer(kind=int32), intent(in) :: x
integer(kind=int32), intent(in) :: lo
integer(kind=int32), intent(in) :: hi

Return Value logical

private elemental function is_in_range_int64(x, lo, hi) result(flag)

Inclusive range test for integer(int64).

Arguments

Type IntentOptional Attributes Name
integer(kind=int64), intent(in) :: x
integer(kind=int64), intent(in) :: lo
integer(kind=int64), intent(in) :: hi

Return Value logical

private elemental function is_in_range_sp(x, lo, hi) result(flag)

Inclusive range test for real(sp).

Arguments

Type IntentOptional Attributes Name
real(kind=sp), intent(in) :: x
real(kind=sp), intent(in) :: lo
real(kind=sp), intent(in) :: hi

Return Value logical