Home > Article > Backend Development > C++ program to calculate log gamma of a given number
The gamma function is described as the expansion of the factorial of each given number math. On the other hand, factorial can only be defined for real numbers, so The gamma function goes beyond computing division Negative integer. It is represented by -
$$\mathrm{\Gamma \left ( x \right )=\left ( x-1 \right )!}$$
The gamma function grows rapidly for higher values; therefore, logarithmically apply Gamma will slow it down significantly. The natural logarithm gamma of a particular number is Another name for it.
In this article we will see how to calculate the logarithm of a given gamma function Enter the number x in C.
C cmath library has a lgamma() function, which accepts parameter x and then executes gamma(x) and apply the natural logarithm to the value. The syntax for using lgamma() is As follows -
#include < cmath > lgamma( <number> )
#include <iostream> #include <cmath> using namespace std; float solve( float x ){ float answer; answer = lgamma( x ); return answer; } int main(){ cout << "Logarithm Gamma for x = 10 is: " << solve( 10 ) << endl; cout << "Logarithm Gamma for 15! which is x = 16 is: " << solve( 16 ) << endl; cout << "Logarithm Gamma for x = -1.2 is: " << solve( -1.2 ) << endl; cout << "Logarithm Gamma for x = 3.1415 is: " << solve( 3.1415 ) << endl; }
Logarithm Gamma for x = 10 is: 12.8018 Logarithm Gamma for 15! which is x = 16 is: 27.8993 Logarithm Gamma for x = -1.2 is: 1.57918 Logarithm Gamma for x = 3.1415 is: 0.827604
C also provides the tgamma() method for the gamma and log() functions. we can use They come to formulate lgamma(). Let's look at the algorithm to get a clear idea.
#include <iostream> #include <cmath> using namespace std; float solve( float x ){ float answer; float g = tgamma( x ); answer = log( g ); return answer; } int main(){ cout << "Logarithm Gamma for x = 10 is: " << solve( 10 ) << endl; cout << "Logarithm Gamma for 15! which is x = 16 is: " << solve( 16 ) << endl; cout << "Logarithm Gamma for x = -1.2 is: " << solve( -1.2 ) << endl; cout << "Logarithm Gamma for x = 3.1415 is: " << solve( 3.1415 ) << endl; }
Logarithm Gamma for x = 10 is: 12.8018 Logarithm Gamma for 15! which is x = 16 is: 27.8993 Logarithm Gamma for x = -1.2 is: 1.57918 Logarithm Gamma for x = 3.1415 is: 0.827604
In the previous example, we saw the use of tgamma() and log() methods. we can Define our factorial() function, but only accept positive numbers. let us see algorithm for better understanding.
To define the factorial function, n
If n is 1, then
Return n
otherwise
Return n * factorial ( n - 1 )
End if
In the main method, use the number x to find the log gamma of x
g := factorial (x - 1)
res := Use log( g ) to find the natural logarithm of g
Return results
#include <iostream> #include <cmath> using namespace std; long fact( int n ){ if( n == 1 ) { return n; } else { return n * fact( n - 1); } } float solve( float x ){ float answer; float g = fact( x - 1 ); answer = log( g ); return answer; } int main(){ cout << "Logarithm Gamma for x = 10 is: " << solve( 10 ) << endl; cout << "Logarithm Gamma for 15! which is x = 16 is: " << solve( 16 ) << endl; cout << "Logarithm Gamma for x = -1.2 is: " << solve( -1.2 ) << endl; }
Logarithm Gamma for x = 10 is: 12.8018 Logarithm Gamma for 15! which is x = 16 is: 27.8993 Segmentation fault (core dumped)
The gamma method is sometimes called an extension of the factorial method. Since the gamma or factorial method grows so quickly, we can use logarithms for it. at this In the article we saw some techniques to perform log gamma on a given number X. Initially, we use the default function, lgamma() from the cmath library in C. The second way is to use tgamma() and log() and finally define our factorial method. However, the final method is limited to positive numbers. it doesn't work with negative numbers number. And it only performs well with integers.
The above is the detailed content of C++ program to calculate log gamma of a given number. For more information, please follow other related articles on the PHP Chinese website!