C/C++ 中的 Power 函式
Power 函式用於計算給定數字的冪。
該 pow 函式計算 a 乘以 b 次方的值,即 ab。
語法
double pow(double a , double b)
它接受 double 整數作為輸入,並輸出 double 整數作為輸出。它pow() 函式在math.h 包中定義。
如果你將一個整數傳給 power 函式,則函式將其轉換為 double 資料型別。但這樣會產生一個問題,有時這種轉換可能會將其儲存為較低的 double。例如,如果我們傳入 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP