anyscalar_to_int64(3f) - [M_anything] convert intrinsic scalar types
to integer(kind=int64)
(LICENSE:MIT)
impure elemental function anyscalar_to_int64(valin) result(value)
class(*),intent(in) :: valin
integer(kind=int64) :: value
This function uses polymorphism to allow arguments of different types
as input. It is typically used to create other procedures that can take
many scalar arguments as input options, equivalent to passing the
parameter VALUE as int(VALUE,0_int64) for integer; nint(VALUE,0_int64)
for real values, returning 0_int64 for .true. and 1_int64 for logical,
and the same as int(VALUE,0_int64) for character variables if the
character variables represent an integer value.
VALUEIN input argument of a procedure to convert to type INTEGER(KIND=int64).
The value of VALUIN converted to INTEGER(KIND=INT64).
Sample program
program demo_anyscalar_to_int64
use, intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
! call same function with many scalar input types
write(*,*)squarei(huge(0_int8)),huge(0_int8) , &
& '16129'
write(*,*)squarei(huge(0_int16)),huge(0_int16) , &
& '1073676289'
write(*,*)squarei(huge(0_int32)),huge(0_int32) , &
& '4611686014132420609'
write(*,*)squarei(huge(0_int64)),huge(0_int64) , &
& '85070591730234615847396907784232501249'
contains
!
function squarei(invalue)
use M_anything, only : anyscalar_to_int64
class(*),intent(in) :: invalue
doubleprecision :: invalue_local
doubleprecision :: squarei
invalue_local=anyscalar_to_int64(invalue)
squarei=invalue_local*invalue_local
end function squarei
!
end program demo_anyscalar_to_int64
Results
16129.000000000000 127 !! 16129
1073676289.0000000 32767 !! 1073676289
4.6116860141324206E+018 2147483647 !! 4611686014132420609
8.5070591730234616E+037 9223372036854775807 !! 85070591730234615847396907784232501249
2.8948022309329049E+076 170141183460469231731687303715884105727 !! 28948022309329048855892746252171976962977213799489202546401021394546514198529
John S. Urban
MIT
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(*), | intent(in) | :: | valuein |
impure elemental function anyscalar_to_int64(valuein) result(ii38) ! ident_6="@(#) M_anything anyscalar_to_int64(3f) convert integer parameter of any kind to 64-bit integer" class(*),intent(in) :: valuein integer(kind=int64) :: ii38 integer :: ios character(len=256) :: message select type(valuein) type is (integer(kind=int8)); ii38=int(valuein,kind=int64) type is (integer(kind=int16)); ii38=int(valuein,kind=int64) type is (integer(kind=int32)); ii38=valuein type is (integer(kind=int64)); ii38=valuein type is (real(kind=real32)); ii38=nint(valuein,kind=int64) type is (real(kind=real64)); ii38=nint(valuein,kind=int64) #ifdef HAS_REAL128 Type is (real(kind=real128)); ii38=nint(valuein,kind=int64) #endif type is (logical); ii38=merge(0_int64,1_int64,valuein) type is (character(len=*)) ; read(valuein,*,iostat=ios,iomsg=message)ii38 if(ios /= 0)then write(stderr,*)'*anyscalar_to_int64* ERROR: '//trim(message) stop 2 endif class default write(stderr,*)'*anyscalar_to_int64* ERROR: unknown integer type' stop 3 end select end function anyscalar_to_int64