當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Math pow()用法及代碼示例

此函數用於查找給定數字的冪。

考慮一個基數 'b' 和 index 'e'。

功率=be

用法

它的語法是:

double pow(double b, double e);
float pow(float b, float e);
long double pow(long double b, long double e);
promoted pow(type1  b, type2 e);

注意:如果任何參數是 long double 類型,則返回類型將提升為 long double。如果不是,則返回類型被提升為 double。

參數

b:'b' 是要計算其冪的數。

e:'e' 是 index 。

返回值

它將基數 'b'raised 返回到 'e' 的冪。

例子1

讓我們看一個簡單的例子,當基數和 index 都是整數類型時。

#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int base=4;
  int exponent=2;
  int power=pow(base,exponent);
  std::cout << "Power of a given number is:" <<power;
  return 0;
}

輸出:

Power of a given number is:16

例子2

讓我們看一個基數為浮點類型而 index 為整數類型的示例。

#include <iostream>
#include<cmath>
using namespace std;
int main()
{
   int base=4.5;
  int exponent=3;
  int power=pow(base,exponent);
  std::cout << "Power of a given number is:" <<power;
  return 0;
}

輸出:

Power of a given number is:64

例子3

讓我們看一個簡單的例子,當基數和 index 都是浮點類型時..

#include <iostream>
#include<cmath>
using namespace std;
int main()
{
   int base=2.5;
  int exponent=2.5;
  int power=pow(base,exponent);
  std::cout << "Power of a given number is:" <<power;
  return 0;
}

輸出:

Power of a given number is:4





相關用法


注:本文由純淨天空篩選整理自 C++ Math pow()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。