EPSILON(3) - [MODEL:NUMERIC] Epsilon function
result = epsilon(x)
real(kind=kind(x)) function epsilon(x)real(kind=kind(x),intent(in) :: x(..)
o X shall be of type real. It may be a scalar or an array. o the result is a scalar of the same type and kind type parameter as X.
EPSILON(3) returns the floating point relative accuracy. It is the nearly negligible number relative to 1 such that 1+ LITTLE_NUMBER is not equal to 1; or more precisely
real( 1.0, kind(x)) + epsilon(x) /= real( 1.0, kind(x))It may be thought of as the distance from 1.0 to the next largest floating point number.
One use of EPSILON(3) is to select a delta value for algorithms that search until the calculation is within delta of an estimate.
If delta is too small the algorithm might never halt, as a computation summing values smaller than the decimal resolution of the data type does not change.
o X : The type shall be real.
The return value is of the same type as the argument.
Sample program:
program demo_epsilon use,intrinsic :: iso_fortran_env, only : dp=>real64,sp=>real32 implicit none real(kind=sp) :: x = 3.143 real(kind=dp) :: y = 2.33d0Results:! so if x is of type real32, epsilon(x) has the value 2**-23 print *, epsilon(x) ! note just the type and kind of x matter, not the value print *, epsilon(huge(x)) print *, epsilon(tiny(x))
! the value changes with the kind of the real value though print *, epsilon(y)
! adding and subtracting epsilon(x) changes x write(*,*)x == x + epsilon(x) write(*,*)x == x - epsilon(x)
! these next two comparisons will be .true. ! write(*,*)x == x + epsilon(x) * 0.999999 write(*,*)x == x - epsilon(x) * 0.999999
! you can calculate epsilon(1.0d0) write(*,*)my_dp_eps()
contains
function my_dp_eps() ! calculate the epsilon value of a machine the hard way real(kind=dp) :: t real(kind=dp) :: my_dp_eps
! starting with a value of 1, keep dividing the value ! by 2 until no change is detected. Note that with ! infinite precision this would be an infinite loop, ! but floating point values in Fortran have a defined ! and limited precision. my_dp_eps = 1.0d0 SET_ST: do my_dp_eps = my_dp_eps/2.0d0 t = 1.0d0 + my_dp_eps if (t <= 1.0d0) exit enddo SET_ST my_dp_eps = 2.0d0*my_dp_eps
end function my_dp_eps end program demo_epsilon
> 1.19209290E-07 > 1.19209290E-07 > 1.19209290E-07 > 2.2204460492503131E-016 > F > F > T > T > 2.2204460492503131E-016
Fortran 95
DIGITS(3), EXPONENT(3), FRACTION(3), HUGE(3), MAXEXPONENT(3), MINEXPONENT(3), NEAREST(3), PRECISION(3), RADIX(3), RANGE(3), RRSPACING(3), SCALE(3), SET_EXPONENT(3), SPACING(3), TINY(3)
Fortran intrinsic descriptions (license: MIT) @urbanjost
Nemo Release 3.1 | epsilon (3fortran) | November 02, 2024 |