lgamma(3)

πŸ’– lgamma(3) β€” Library Functions Manual πŸ’–

okay bestie, sit down, hydrate, and get ready, because we're doing gamma functions today. this one lives in section 3 of the manual (that's the library-functions section, iykyk), and it's genuinely iconic once you stop being scared of it. no cap. ✨


🌸 NAME 🌸

lgamma, lgammaf, lgammal, lgamma_r, lgammaf_r, lgammal_r, signgam β€” log gamma function πŸ¦‹


πŸŽ€ LIBRARY πŸŽ€

Math library (libm, link it with -lm). yeah girl, you gotta link it. -lm or it's not happening. πŸ’…


✨ SYNOPSIS ✨

okay so first thing, you gotta pull in the header:

#include <math.h>

then here's the whole girl group β€” the plain float-suffixed family:

double lgamma(double x);
float lgammaf(float x);
long double lgammal(long double x);

and the reentrant besties who take a pointer instead of being all weird and global about it:

double lgamma_r(double x, int *signp);
float lgammaf_r(float x, int *signp);
long double lgammal_r(long double x, int *signp);

and then the one that is just... out there, existing, globally, all the time:

extern int signgam;

🧚 Feature Test Macro Requirements for glibc (see feature_test_macros(7)) 🧚

for lgamma():

_ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE
    || /* Since glibc 2.19: */ _DEFAULT_SOURCE
    || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE

for lgammaf(), lgammal():

_ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
    || /* Since glibc 2.19: */ _DEFAULT_SOURCE
    || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE

for lgamma_r(), lgammaf_r(), lgammal_r():

/* Since glibc 2.19: */ _DEFAULT_SOURCE
    || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE

for signgam:

_XOPEN_SOURCE
    || /* Since glibc 2.19: */ _DEFAULT_SOURCE
    || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE

feature test macros are the bouncers at the club btw. you don't have the right define, you don't get in. πŸ’€


🌈 DESCRIPTION 🌈

so for the actual definition of the Gamma function, go read tgamma(3) β€” that's where that lore lives, we're not rehashing it here.

lgamma(), lgammaf(), and lgammal() return the natural logarithm of the absolute value of the Gamma function. the absolute value!! that's the whole flex. the sign is not lost though, she just gets reported somewhere else β€” in the external integer signgam, declared in <math.h>. it's 1 when the Gamma function is positive or zero, and -1 when it's negative. so it's literally just vibes: is it a 1 or a -1. πŸ‘‘

now here's the tea: signgam is one single constant location, and using a constant location is not thread-safe. 😭 so the lgamma_r(), lgammaf_r(), and lgammal_r() girlies were introduced β€” they hand the sign back to you through the argument signp instead. no shared global, no drama, no race condition. slay. ✨


πŸ’œ RETURN VALUE πŸ’œ

on success, these functions return the natural logarithm of Gamma(x).

  • if x is a NaN, a NaN is returned. (NaN in, NaN out, the circle of life πŸ¦‹)
  • if x is 1 or 2, +0 is returned. (cute, lowkey free)
  • if x is positive infinity or negative infinity, positive infinity is returned.
  • if x is a nonpositive integer, a pole error occurs, and the functions return +HUGE_VAL, +HUGE_VALF, or +HUGE_VALL, respectively.
  • if the result overflows, a range error occurs, and the functions return HUGE_VAL, HUGE_VALF, or HUGE_VALL, respectively, with the correct mathematical sign.

🫢 ERRORS 🫢

for how to even tell whether an error happened when you called these, check math_error(7). that's the detective manual, bestie. πŸ”

the following errors can occur:

  • Pole error: x is a nonpositive integer β€” errno is set to ERANGE (but see BUGS below, girl, it's a whole thing). a divide-by-zero floating-point exception (FE_DIVBYZERO) is raised. πŸ’₯
  • Range error: result overflow β€” errno is set to ERANGE. an overflow floating-point exception (FE_OVERFLOW) is raised. 🌊

⭐ STANDARDS ⭐

  • lgamma()
  • lgammaf()
  • lgammal()

β†’ C11, POSIX.1-2008. certified, standardized, gorgeous. πŸ’…

  • signgam

β†’ POSIX.1-2008.

  • lgamma_r()
  • lgammaf_r()
  • lgammal_r()

β†’ None. (they're the cool underground indie ones, no official backing 😌)


πŸ¦‹ HISTORY πŸ¦‹

  • lgamma()
  • lgammaf()
  • lgammal()

β†’ C99, POSIX.1-2001.

  • signgam

β†’ POSIX.1-2001.

  • lgamma_r()
  • lgammaf_r()
  • lgammal_r()

β†’ None.


πŸ’€ BUGS πŸ’€

bestie. listen. in glibc 2.9 and earlier, when a pole error occurs, errno is set to EDOM β€” instead of the POSIX-mandated ERANGE. yes, that is wrong. yes, they knew. since glibc 2.10, glibc does the right thing. so if you're on truly ancient glibc, that's on you, not on us. πŸ™ˆ


πŸ‘‘ SEE ALSO πŸ‘‘


and that's the whole page, bestie!! πŸŽ€βœ¨ go forth and compute logarithms of gamma functions responsibly. and remember: it's absolute value of gamma, the sign goes in signgam (or in *signp if you're using the _r ones like a responsible queen). πŸ‘‘

this was Linux man-pages 6.20, dated 2026-02-08 β€” mwah, love that for us. πŸ’–πŸ¦‹πŸŒΈπŸ’…

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms