FU_Numbers Module

Functions to analyze numbers in Fortran programs. Some of these functions (is_nan and is_inf) are now available in the intrinsics module IEEE_ARITHMETIC and are provided here only for compatibility with some old programs that use them.



Contents


Interfaces

public interface count_digits_integer

Counts the number of digits of an integer, including the - sign in case it is a negative value.

Syntax

 n = count_digits_integer(i)

Where:

  • i: Integer number whose digits are to be counted

It returns the number of digits of the input number, including the - sign in case it is a negative value.

Example

The following program prints the number of digits of some integer numbers:

 PROGRAM count_digits_integerExample
    USE FU_Numbers, ONLY: count_digits_integer
    IMPLICIT NONE
    WRITE(*,*) count_digits_integer(1234)
    WRITE(*,*) count_digits_integer(-1234)
 END PROGRAM count_digits_integerExample
  • private pure function count_digits_integer_i8(i) result(num_digits)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=i8), intent(in) :: i

    Integer number whose digits are to be counted.

    Return Value integer(kind=i8)

    The number of digits of the input number.

  • private pure function count_digits_integer_i16(i) result(num_digits)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=i16), intent(in) :: i

    Integer number whose digits are to be counted.

    Return Value integer(kind=i16)

    The number of digits of the input number.

  • private pure function count_digits_integer_i32(i) result(num_digits)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=i32), intent(in) :: i

    Integer number whose digits are to be counted.

    Return Value integer(kind=i32)

    The number of digits of the input number.

  • private pure function count_digits_integer_i64(i) result(num_digits)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=i64), intent(in) :: i

    Integer number whose digits are to be counted.

    Return Value integer(kind=i64)

    The number of digits of the input number.

public interface is_nan

Determines if the value of the input variable is NaN.

Syntax

 n = is_nan(f)

Where:

  • f: Real number to check if it is NaN.

It returns True if the number is NaN and False otherwise.

Example

The following program checks some real numbers to see if they are NaN:

 PROGRAM is_nanExample
    USE FU_Numbers, ONLY: is_nan
    IMPLICIT NONE
    REAL :: f
    WRITE(*,*) is_nan(5.)
    f = 0.
    WRITE(*,*) is_nan(1/f)
    WRITE(*,*) is_nan(f/f)
 END PROGRAM is_nanExample
  • private elemental function is_nan_sp(val) result(res)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=sp), intent(in) :: val

    Value to analize. It can have any rank and dimension

    Return Value logical

    True if the variable is NaN. False otherwise. It will have the same rank and dimension as the input value.

  • private elemental function is_nan_dp(val) result(res)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=dp), intent(in) :: val

    Value to analize. It can have any rank and dimension

    Return Value logical

    True if the variable is NaN. False otherwise. It will have the same rank and dimension as the input value.

  • private elemental function is_nan_qp(val) result(res)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=qp), intent(in) :: val

    Value to analize. It can have any rank and dimension

    Return Value logical

    True if the variable is NaN. False otherwise. It will have the same rank and dimension as the input value.

public interface is_inf

Determines if the value of the input variable is Infinity.

Syntax

 n = is_inf(f)

Where:

  • f: Real number to check if it is Infinity.

It returns True if the number is Infinity and False otherwise.

Example

The following program checks some real numbers to see if they are Infinity:

 PROGRAM is_infExample
    USE FU_Numbers, ONLY: is_inf
    IMPLICIT NONE
    REAL :: f
    WRITE(*,*) is_inf(5.)
    f = 0.
    WRITE(*,*) is_inf(1./f)
    WRITE(*,*) is_inf(f/f)
 END PROGRAM is_infExample
  • private elemental function is_inf_sp(val) result(res)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=sp), intent(in) :: val

    Value to analize. It can have any rank and dimension

    Return Value logical

    True if the variable is Inf. False otherwise. It will have the same rank and dimension as the input value.

  • private elemental function is_inf_dp(val) result(res)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=dp), intent(in) :: val

    Value to analize. It can have any rank and dimension

    Return Value logical

    True if the variable is Inf. False otherwise. It will have the same rank and dimension as the input value.

  • private elemental function is_inf_qp(val) result(res)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=qp), intent(in) :: val

    Value to analize. It can have any rank and dimension

    Return Value logical

    True if the variable is Inf. False otherwise. It will have the same rank and dimension as the input value.

public interface eq

Tests two real numberes for equality using a tolerance if provided by the user, or selecting a tolerance automatically otherwise.

Syntax

 n = eq(f1, f2, eps)

Where:

  • f1: First real number to compare for equality.
  • f2: Second real to compare for equality.
  • eps: User selected tolerance for the comparison. If not provided it will be selected automatically.

It returns True both numbers are equal according to the selected tolerance and False otherwise

Example

The following program tests if two real numbers are equal:

 PROGRAM eqExample
    USE FU_Numbers, ONLY: eq
    IMPLICIT NONE
    WRITE(*,*) eq(5., 5.00001, 0.000000001)
    WRITE(*,*) eq(5., 5.00001, 0.001)
    WRITE(*,*) eq(5., 5.00001)
 END PROGRAM eqExample
  • private elemental function eq_sp(x1, x2, eps) result(res)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=sp), intent(in) :: x1

    First real value to compare for equality.

    real(kind=sp), intent(in) :: x2

    Second real value to compare for equality.

    real(kind=sp), intent(in), optional :: eps

    User selected tolerance for the comparison. If not provided it will be selected automatically.

    Return Value logical

    True if both numbers are equal according to the selected tolerance. False otherwise.

  • private elemental function eq_dp(x1, x2, eps) result(res)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=dp), intent(in) :: x1

    First real value to compare for equality.

    real(kind=dp), intent(in) :: x2

    Second real value to compare for equality.

    real(kind=dp), intent(in), optional :: eps

    User selected tolerance for the comparison. If not provided it will be selected automatically.

    Return Value logical

    True if both numbers are equal according to the selected tolerance. False otherwise.

  • private elemental function eq_qp(x1, x2, eps) result(res)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=qp), intent(in) :: x1

    First real value to compare for equality.

    real(kind=qp), intent(in) :: x2

    Second real value to compare for equality.

    real(kind=qp), intent(in), optional :: eps

    User selected tolerance for the comparison. If not provided it will be selected automatically.

    Return Value logical

    True if both numbers are equal according to the selected tolerance. False otherwise.

public interface ne

Tests two real numberes for inequality using a tolerance if provided by the user, or selecting a tolerance automatically otherwise.

Syntax

 n = ne(f1, f2, eps)

Where:

  • f1: First real number to compare for inequality.
  • f2: Second real to compare for inequality.
  • eps: User selected tolerance for the comparison. If not provided it will be selected automatically.

It returns True both numbers are not equal according to the selected tolerance and False otherwise

Example

The following program tests if two real numbers are not equal:

 PROGRAM neExample
    USE FU_Numbers, ONLY: ne
    IMPLICIT NONE
    WRITE(*,*) ne(5., 5.00001, 0.000000001)
    WRITE(*,*) ne(5., 5.00001, 0.001)
    WRITE(*,*) ne(5., 5.00001)
 END PROGRAM neExample
  • private elemental function ne_sp(x1, x2, eps) result(res)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=sp), intent(in) :: x1

    First real value to compare for inequality.

    real(kind=sp), intent(in) :: x2

    Second real value to compare for inequality.

    real(kind=sp), intent(in), optional :: eps

    User selected tolerance for the comparison. If not provided it will be selected automatically.

    Return Value logical

    True if the numbers are not equal according to the selected tolerance. False otherwise.

  • private elemental function ne_dp(x1, x2, eps) result(res)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=dp), intent(in) :: x1

    First real value to compare for inequality.

    real(kind=dp), intent(in) :: x2

    Second real value to compare for inequality.

    real(kind=dp), intent(in), optional :: eps

    User selected tolerance for the comparison. If not provided it will be selected automatically.

    Return Value logical

    True if the numbers are not equal according to the selected tolerance. False otherwise.

  • private elemental function ne_qp(x1, x2, eps) result(res)

    Arguments

    Type IntentOptional Attributes Name
    real(kind=qp), intent(in) :: x1

    First real value to compare for inequality.

    real(kind=qp), intent(in) :: x2

    Second real value to compare for inequality.

    real(kind=qp), intent(in), optional :: eps

    User selected tolerance for the comparison. If not provided it will be selected automatically.

    Return Value logical

    True if the numbers are not equal according to the selected tolerance. False otherwise.