首頁  >  文章  >  後端開發  >  C/C++中的Power函數

C/C++中的Power函數

PHPz
PHPz轉載
2023-09-02 21:25:031398瀏覽

C/C++中的Power函數

Power function is used to calculate the power of the given number.

The pow function find the value of a raised to the power b i.e. ab #.

Syntax

double pow(double a , double b)

它接受雙整數作為輸入,並將雙整數作為輸出。它的pow()函數在math.h套件中定義。

如果將整數傳遞給冪函數,函數會將其轉換為雙精確度資料類型。但是這裡存在一個問題,有時這種轉換可能會將其儲存為較低的雙精度數。例如,如果我們傳遞3並將其轉換為2.99,那麼平方是8.99940001,轉換為8。但這是一個錯誤,雖然它很少發生,但為了消除這個錯誤,將其加上0.25。

範例程式碼

#include <stdio.h>
#include <math.h>
int main() {
   double x = 6.1, y = 2;
   double result = pow(x, y);
   printf("%f raised to the power of %f is %f \n" ,x,y, result );
   // Taking integers
   int a = 5 , b = 2;
   int square = pow(a,b);
   printf("%d raised to the power of %d is %d \n", a,b, square );
   return 0;
}

輸出

6.100000 raised to the power of 2.000000 is 37.210000
5 raised to the power of 2 is 25

以上是C/C++中的Power函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除