Search code examples
fortrangfortranexp

expm1 for GNU gfortran


Is there any way to call a fast implementation of expm1 from GNU Fortran? Ideally, it would be great to have a function to calculate (exp(x)-1)/x directly to avoid extra check for zero argument. Elemental version of expm1 would be especially helpful.


Solution

  • This is how it's called from libm:

      use, intrinsic :: iso_c_binding, only: c_double
    
      implicit none
    
      interface
         real(c_double) function expm1(x) bind(c, name='expm1')
           import c_double
           real(c_double), intent(in), value :: x
         end function expm1
      end interface
    
      print*, expm1(3.4d0)
    
    end program
    

    If the glibc source code for the function does not look too discouraging then you might wish to translate it into Fortran in order to make it elemental (if by elemental you meant the Fortran keyword).