首頁 > 後端開發 > C++ > 主體

C/C++中的Power函數

PHPz
發布: 2023-09-02 21:25:03
轉載
1427 人瀏覽過

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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!