伽马函数被描述为每个给定数字的阶乘的扩展数学。另一方面,阶乘只能为实数定义,因此gamma 函数超出了计算除负整数。它由 –
表示
$$mathrm{Gamma left ( x ight )=left ( x-1 ight )!}$$
伽玛函数对于更高的值会快速增长;因此,对数应用对数伽玛会大大减慢它的速度。特定数字的自然对数 gamma 为它的另一个名字。
在本文中,我们将了解如何计算给定的伽玛函数的对数在C++中输入数字x。
使用 lgamma() 函数对数 Gamma
C++ cmath 库有一个 lgamma() 函数,它接受参数 x,然后执行gamma(x) 并对该值应用自然对数。使用 lgamma() 的语法是如下所示 –
语法
#include lgamma( )
登录后复制
算法
读取数字 xres := 使用 lgamma( x ) 的对数 gamma返回结果
示例
#include #include using namespace std;float solve( float x ){ float answer; answer = lgamma( x ); return answer;}int main(){ cout输出
Logarithm Gamma for x = 10 is: 12.8018Logarithm Gamma for 15! which is x = 16 is: 27.8993Logarithm Gamma for x = -1.2 is: 1.57918Logarithm Gamma for x = 3.1415 is: 0.827604登录后复制
使用 gamma() 和 log() 函数
C++ 还为 gamma 和 log() 函数提供了 tgamma() 方法。我们可以用他们来制定 lgamma()。让我们看看算法以获得清晰的想法。
算法
读取数字 xg := 使用 tgamma( x ) 计算 gammares := 使用 log( g ) 的对数 gamma返回结果
示例
#include #include 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: 12.8018Logarithm Gamma for 15! which is x = 16 is: 27.8993Logarithm Gamma for x = -1.2 is: 1.57918Logarithm Gamma for x = 3.1415 is: 0.827604登录后复制
使用 Factorial() 和 log() 函数
在上一个示例中,我们看到了 tgamma() 和 log() 方法的使用。我们可以定义我们的阶乘()函数,但这只接受正数。让我们看看算法以便更好地理解。
算法
定义阶乘函数,需要 n
如果 n 为 1,则
返回n
立即学习“C++免费学习笔记(深入)”;
否则
返回 n * 阶乘 ( n - 1 )
立即学习“C++免费学习笔记(深入)”;
结束如果
在 main 方法中,用数字 x 求 x 的 log gamma
g := 阶乘( x - 1)
res := 使用 log( g ) 求 g 的自然对数
返回结果
示例
#include #include 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: 12.8018Logarithm Gamma for 15! which is x = 16 is: 27.8993Segmentation fault (core dumped)登录后复制
结论
伽马方法有时被称为阶乘方法的扩展。由于伽玛或阶乘方法增长得如此之快,我们可以对其使用对数。在这个文章中,我们看到了一些对给定数字执行对数伽玛的技术X。最初,我们使用默认函数,即 C++ 中 cmath 库中的 lgamma()。第二种方法是使用 tgamma() 和 log(),最后定义我们的阶乘方法。然而,最终方法仅限于正数。它不适用于负数数字。而且它只对整数表现良好。
以上就是C++程序用于计算给定数字的对数伽玛的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2588078.html