Calling the lgamma function from the Intel Fortran Compiler

Problem :

If your Fortran source calls the 'LGAMMA' function, it is not accepted by the Intel Fortran compiler versions 10 and 11.
In C99, lgamma() returns the logarithm of the absolute value of the gamma function.


Windows, Linux, Mac OS X

Root Cause :

Functions such as lgamma and erfc are defined in the GNU library glibc as language extensions (see the reference to intro(3f)).  lgamma is part of the C99 standard, but not the Fortran standard.  Optimized versions of these functions are implemented in the Intel Compiler's math runtime library (libimf on Linux and Mac OS X, libm on Windows).

However, the Intel math library only provides C entry points for lgamma, but no Fortran entry points at this time.

Resolution :

You may call these functions from Fortran if you are careful to use the right calling conventions between Fortran and C.

Here is a sample Fortran code for your reference. It uses the ISO_C_BINDING module that is new in the Fortran 2003 standard.

program test_lgamma
  implicit real(c_double) (a-h, o-z)

    real(c_double) function lgamma (y) bind(c)
      use iso_c_binding
      real(c_double), value :: y
  end interface

  do i = 1,8
    x = 0.5 * i
    g2 = lgamma(x)

    print '("lgamma(", f5.2, " ) =", f20.15, "    gamma =", F20.15)', x, g2, exp(g2)


# Note: Please remember you need to specify explicitly that the argument is passed by value (in Line 8)
