Bug 1837 - tgamma() is incorrectly implemented
Summary: tgamma() is incorrectly implemented
Status: NEW
Alias: None
Product: uClibc
Classification: Unclassified
Component: Standard Compliance (show other bugs)
Version: 0.9.32
Hardware: All Linux
: P5 major
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-25 22:18 UTC by r.c.poss
Modified: 2015-04-16 08:02 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description r.c.poss 2010-05-25 22:18:14 UTC
As per the ISO standard, tgamma(x) should compute Gamma(x), while lgamma(x) computes ln(Gamma(x)). The current implementation aliases tgamma and lgamma, which is incorrect.

This was identified in Sept 2009, as per the comment in e_lgamma_r.c:

"""
/* FIXME! Looks like someone just used __ieee754_gamma_r,
 * believing it's a "true" gamma function, but it was not!
 * Our tgamma is WRONG.
 */
"""

(Denis Vlasenko, 2009-09-18)

The function tgamma() thus needs to be replaced entirely.

A "quick and dirty" fix is to rewrite as follows:

"""
        double y;
        int local_signgam;
        y = __ieee754_lgamma_r(x, &local_signgam);
        return local_signgam * exp(y);
"""

This provides relatively accurate results for small inputs, but diverges for large inputs.

An alternate implementation can be found in FreeBSD's source, in /scratch/usr/src/lib/msun/bsdsrc/b_tgamma.c.
Comment 1 Bernhard Reutner-Fischer 2011-02-09 19:40:35 UTC
Care to submit a tested, proper patch?
See http://uclibc.org/developing.html#contrib for instructions.

Thanks!