C++程序用于计算给定数字的对数伽玛

WBOY
WBOY 转载
2023-08-25 15:13:06 1151浏览

C++程序用于计算给定数字的对数伽玛

伽马函数被描述为每个给定数字的阶乘的扩展 数学。另一方面,阶乘只能为实数定义,因此 gamma 函数超出了计算除 负整数。它由 -

表示

$$\mathrm{\Gamma \left ( x \right )=\left ( x-1 \right )!}$$

伽玛函数对于更高的值会快速增长;因此,对数应用对数 伽玛会大大减慢它的速度。特定数字的自然对数 gamma 为 它的另一个名字。

在本文中,我们将了解如何计算给定的伽玛函数的对数 在C++中输入数字x。

使用 lgamma() 函数对数 Gamma

C++ cmath 库有一个 lgamma() 函数,它接受参数 x,然后执行 gamma(x) 并对该值应用自然对数。使用 lgamma() 的语法是 如下所示 -

语法

#include < cmath >
lgamma( <number> )

算法

  • 读取数字 x
  • res := 使用 lgamma( x ) 的对数 gamma
  • 返回结果

示例

#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

使用 gamma() 和 log() 函数

C++ 还为 gamma 和 log() 函数提供了 tgamma() 方法。我们可以用 他们来制定 lgamma()。让我们看看算法以获得清晰的想法。

算法

  • 读取数字 x
  • g := 使用 tgamma( x ) 计算 gamma
  • res := 使用 log( g ) 的对数 gamma
  • 返回结果

示例

#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

使用 Factorial() 和 log() 函数

在上一个示例中,我们看到了 tgamma() 和 log() 方法的使用。我们可以 定义我们的阶乘()函数,但这只接受正数。让我们看看 算法以便更好地理解。

算法

  • 定义阶乘函数,需要 n

  • 如果 n 为 1,则

    • 返回n

  • 否则

    • 返回 n * 阶乘 ( n - 1 )

  • 结束如果

  • 在 main 方法中,用数字 x 求 x 的 log gamma

  • g := 阶乘( x - 1)

  • res := 使用 log( g ) 求 g 的自然对数

  • 返回结果

示例

#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)

结论

伽马方法有时被称为阶乘方法的扩展。 由于伽玛或阶乘方法增长得如此之快,我们可以对其使用对数。在这个 文章中,我们看到了一些对给定数字执行对数伽玛的技术 X。最初,我们使用默认函数,即 C++ 中 cmath 库中的 lgamma()。 第二种方法是使用 tgamma() 和 log(),最后定义我们的阶乘方法。 然而,最终方法仅限于正数。它不适用于负数 数字。而且它只对整数表现良好。

以上就是C++程序用于计算给定数字的对数伽玛的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除