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).

The Intel math library only provides C entry points for lgamma, but no Fortran entry points. Instead, the Fortran standard entry point LOG_GAMMA is supported from the version 12 compiler onward.

Resolution :

You may call functions such as these 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. This method can be used for calling other C functions from Fortran.

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)


Para obtener información más completa sobre las optimizaciones del compilador, consulte nuestro Aviso de optimización.