source: trunk/libs/newlib/src/newlib/libm/math/w_gamma.c @ 543

Last change on this file since 543 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 5.7 KB
Line 
1
2/* @(#)w_gamma.c 5.1 93/09/24 */
3/*
4 * ====================================================
5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 *
7 * Developed at SunPro, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
10 * is preserved.
11 * ====================================================
12 *
13 */
14
15/* BUG:  FIXME?
16     According to Linux man pages for tgamma, lgamma, and gamma, the gamma
17function was originally defined in BSD as implemented here--the log of the gamma
18function.  BSD 4.3 changed the name to lgamma, apparently removing gamma.  BSD
194.4 re-introduced the gamma name with the more intuitive, without logarithm,
20plain gamma function.  The C99 standard apparently wanted to avoid a problem
21with the poorly-named earlier gamma and used tgamma when adding a plain
22gamma function.
23     So the current gamma is matching an old, bad definition, and not
24matching a newer, better definition.  */
25/*
26FUNCTION
27        <<gamma>>, <<gammaf>>, <<lgamma>>, <<lgammaf>>, <<gamma_r>>, <<gammaf_r>>, <<lgamma_r>>, <<lgammaf_r>>, <<tgamma>>, and <<tgammaf>>---logarithmic and plain gamma functions
28
29INDEX
30gamma
31INDEX
32gammaf
33INDEX
34lgamma
35INDEX
36lgammaf
37INDEX
38gamma_r
39INDEX
40gammaf_r
41INDEX
42lgamma_r
43INDEX
44lgammaf_r
45INDEX
46tgamma
47INDEX
48tgammaf
49
50SYNOPSIS
51#include <math.h>
52double gamma(double <[x]>);
53float gammaf(float <[x]>);
54double lgamma(double <[x]>);
55float lgammaf(float <[x]>);
56double gamma_r(double <[x]>, int *<[signgamp]>);
57float gammaf_r(float <[x]>, int *<[signgamp]>);
58double lgamma_r(double <[x]>, int *<[signgamp]>);
59float lgammaf_r(float <[x]>, int *<[signgamp]>);
60double tgamma(double <[x]>);
61float tgammaf(float <[x]>);
62
63DESCRIPTION
64<<gamma>> calculates
65@tex
66$\mit ln\bigl(\Gamma(x)\bigr)$,
67@end tex
68the natural logarithm of the gamma function of <[x]>.  The gamma function
69(<<exp(gamma(<[x]>))>>) is a generalization of factorial, and retains
70the property that
71@ifnottex
72<<exp(gamma(N))>> is equivalent to <<N*exp(gamma(N-1))>>.
73@end ifnottex
74@tex
75$\mit \Gamma(N)\equiv N\times\Gamma(N-1)$.
76@end tex
77Accordingly, the results of the gamma function itself grow very
78quickly.  <<gamma>> is defined as
79@tex
80$\mit ln\bigl(\Gamma(x)\bigr)$ rather than simply $\mit \Gamma(x)$
81@end tex
82@ifnottex
83the natural log of the gamma function, rather than the gamma function
84itself,
85@end ifnottex
86to extend the useful range of results representable.
87
88The sign of the result is returned in the global variable <<signgam>>,
89which is declared in math.h.
90
91<<gammaf>> performs the same calculation as <<gamma>>, but uses and
92returns <<float>> values.
93
94<<lgamma>> and <<lgammaf>> are alternate names for <<gamma>> and
95<<gammaf>>.  The use of <<lgamma>> instead of <<gamma>> is a reminder
96that these functions compute the log of the gamma function, rather
97than the gamma function itself.
98
99The functions <<gamma_r>>, <<gammaf_r>>, <<lgamma_r>>, and
100<<lgammaf_r>> are just like <<gamma>>, <<gammaf>>, <<lgamma>>, and
101<<lgammaf>>, respectively, but take an additional argument.  This
102additional argument is a pointer to an integer.  This additional
103argument is used to return the sign of the result, and the global
104variable <<signgam>> is not used.  These functions may be used for
105reentrant calls (but they will still set the global variable <<errno>>
106if an error occurs).
107
108<<tgamma>> and <<tgammaf>> are the "true gamma" functions, returning
109@tex
110$\mit \Gamma(x)$,
111@end tex
112the gamma function of <[x]>--without a logarithm.
113(They are apparently so named because of the prior existence of the old,
114poorly-named <<gamma>> functions which returned the log of gamma up
115through BSD 4.2.)
116
117RETURNS
118Normally, the computed result is returned.
119
120When <[x]> is a nonpositive integer, <<gamma>> returns <<HUGE_VAL>>
121and <<errno>> is set to <<EDOM>>.  If the result overflows, <<gamma>>
122returns <<HUGE_VAL>> and <<errno>> is set to <<ERANGE>>.
123
124You can modify this error treatment using <<matherr>>.
125
126PORTABILITY
127Neither <<gamma>> nor <<gammaf>> is ANSI C.  It is better not to use either
128of these; use <<lgamma>> or <<tgamma>> instead.@*
129<<lgamma>>, <<lgammaf>>, <<tgamma>>, and <<tgammaf>> are nominally C standard
130in terms of the base return values, although the <<matherr>> error-handling
131is not standard, nor is the <[signgam]> global for <<lgamma>>.
132*/
133
134/* double gamma(double x)
135 * Return the logarithm of the Gamma function of x.
136 *
137 * Method: call gamma_r
138 */
139
140#include "fdlibm.h"
141#include <reent.h>
142#include <errno.h>
143
144#ifndef _DOUBLE_IS_32BITS
145
146#ifdef __STDC__
147        double gamma(double x)
148#else
149        double gamma(x)
150        double x;
151#endif
152{
153#ifdef _IEEE_LIBM
154        return __ieee754_gamma_r(x,&(_REENT_SIGNGAM(_REENT)));
155#else
156        double y;
157        struct exception exc;
158        y = __ieee754_gamma_r(x,&(_REENT_SIGNGAM(_REENT)));
159        if(_LIB_VERSION == _IEEE_) return y;
160        if(!finite(y)&&finite(x)) {
161#ifndef HUGE_VAL
162#define HUGE_VAL inf
163            double inf = 0.0;
164
165            SET_HIGH_WORD(inf,0x7ff00000);      /* set inf to infinite */
166#endif
167            exc.name = "gamma";
168            exc.err = 0;
169            exc.arg1 = exc.arg2 = x;
170            if (_LIB_VERSION == _SVID_)
171                exc.retval = HUGE;
172            else
173                exc.retval = HUGE_VAL;
174            if(floor(x)==x&&x<=0.0) {
175                /* gamma(-integer) or gamma(0) */
176                exc.type = SING;
177                if (_LIB_VERSION == _POSIX_)
178                  errno = EDOM;
179                else if (!matherr(&exc)) {
180                  errno = EDOM;
181                }
182            } else {
183                /* gamma(finite) overflow */
184                exc.type = OVERFLOW;
185                if (_LIB_VERSION == _POSIX_)
186                  errno = ERANGE;
187                else if (!matherr(&exc)) {
188                  errno = ERANGE;
189                }
190            }
191            if (exc.err != 0)
192               errno = exc.err;
193            return exc.retval; 
194        } else
195            return y;
196#endif
197}             
198
199#endif /* defined(_DOUBLE_IS_32BITS) */
Note: See TracBrowser for help on using the repository browser.