C++ 數學函式


C++ 程式語言可以使用包含在 **math 或 cmath** 庫中的數學函式進行數學計算。這些數學函式被定義用於執行復雜的數學計算。讓我們逐一學習每個函式。

正弦

sin 方法用於計算以度為單位給定角度的正弦值。此函式接受一個雙精度整數作為引數,並返回一個雙精度整數,該整數是 sin(x°) 的值。

double sin(double)

呼叫語法

double x = sin(23.4);

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 45.3;
   cout << "sin ( "<<x<<" ) = " << sin(x) << endl;
}

輸出

sin( 45.3 ) = 0.968142

餘弦

cosin 或 cos 方法用於計算以度為單位給定角度的餘弦值。此函式接受一個雙精度整數作為引數,並返回一個雙精度整數,該整數是 cos(x°) 的值。

double cos(double)

呼叫語法

double x = cos(23.4);

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 45.3;
   cout << "cos ( "<<x<<" ) = " << cos(x) << endl;
}

輸出

cos( 45.3 ) = 0.2504

正切

正切或 tan 方法用於計算以度為單位給定角度的正切值。此函式接受一個雙精度整數作為引數,並返回一個雙精度整數,該整數是 tan(xo) = sin(x°)∕cos(x°) 的值。

double tan(double)

呼叫語法

double x = tan(23.4);

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 45.3;
   cout << "tan ( "<<x<<" ) = " << tan(x) << endl;
}

輸出

tan( 45.3 ) = 3.86638

反正弦

asin 函式用於查詢給定引數的反正弦值。它返回給定輸入集的 asin 值,該輸入集可以是 -1 到 1 之間的任何雙精度整數,否則會報錯。該函式接受 -1 到 1 之間的雙精度整數,並返回一個雙精度值作為 asin() 的結果。

double asin(double)

呼叫語法

double x = asin(0.3232);

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 0.3232;
   cout << "asin ( "<<x<<" ) = " << asin(x) << endl;
}

輸出

asin( 0.3232 ) = 0.3291

反餘弦

acos 函式用於查詢給定引數的反餘弦值。它返回給定輸入集的 acos 值,該輸入集可以是 -1 到 1 之間的任何雙精度整數,否則會報錯。該函式接受 -1 到 1 之間的雙精度整數,並返回一個雙精度值作為 acos() 的結果。

double acos(double)

呼叫語法

double x = acos(0.3232);

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 0.3232;
   cout << "acos ( "<<x<<" ) = " << acos(x) << endl;
}

輸出

acos( 0.3232 ) = 1.24169

反正切

atan 函式用於計算給定引數的反正切值。它返回引數中雙精度輸入值的 atan 的雙精度值。

double atan(double)

呼叫語法

double x = atan(0.3232);

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 0.3232;
   cout << "atan ( "<<x<<" ) = " << atan(x) << endl;
}

輸出

atan( 0.3232 ) = 0.312603

雙曲餘弦

cosh 函式用於計算給定引數的雙曲餘弦值。它返回引數中雙精度輸入值的 cosh 的雙精度值。

double cosh(double)

呼叫語法

double x = cosh(0.342);

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 0.342;
   cout << "cosh ( "<<x<<" ) = " << cosh(x) << endl;
}

輸出

cosh( 0.342 ) = 1.05905

雙曲正弦

sinh 函式用於計算給定引數的雙曲正弦值。它返回引數中雙精度輸入值的 sinh 的雙精度值。

double sinh(double)

呼叫語法

double x = sinh(0.342);

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 0.342;
   cout << "sinh ( "<<x<<" ) = " << sinh(x) << endl;
}

輸出

sinh( 0.342 ) = 0.348706

雙曲正切

tanh 函式用於計算給定引數的雙曲正切值。它返回引數中雙精度輸入值的 sinh 的雙精度值。

double tanh(double)

呼叫語法

double x = tanh(0.342);

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double x = 0.342;
   cout << "tanh ( "<<x<<" ) = " << tanh(x) << endl;
}

輸出

tanh( 0.342 ) = 0.329262

pow 函式用於計算以指數為冪的底數的冪。它接受兩個雙精度值作為引數,這兩個引數分別是底數和指數,它返回一個雙精度整數,該整數是底數的指數次冪。

double pow( double, double)

呼叫語法

double x = pow(2, 4)

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double base = 2 , power = 4;
   cout << "pow( "<<base<<" , "<<power<<" ) = " << pow(base, power) << endl;
}

輸出

pow( 2 , 4 ) = 16

Sqrt(平方根)

C++ 中的 sqrt 函式返回引數列表中雙精度整數的平方根。該方法接受一個雙精度整數值作為輸入,查詢平方根並返回一個雙精度整數值作為輸出。

double sqrt( double)

呼叫語法

double x = sqrt(25.00)

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double a =25 ;
   cout << "sqrt( "<<a<<" ) = " << sqrt(a) << endl;
}

輸出

sqrt( 25 ) = 5.00

對數

lock 函式用於查詢給定數字的自然對數。此方法接受單個雙精度整數值,查詢對數值,並返回 log() 的雙精度整數結果。

double log( double)

呼叫語法

double x = log(1.35)

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double a =1.35 ;
   cout << "sqrt( "<<a<<" ) = " << sqrt(a) << endl;
}

輸出

sqrt( 1.35 ) = 0.300105

向下取整

floor 函式用於返回給定雙精度整數的向下取整值。向下取整值表示四捨五入後的值。該函式接受一個雙精度值作為輸入,並返回使用 floor() 計算的雙精度整數值。

double floor( double)

呼叫語法

double x = floor(5.24)

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double a =6.24 ;
   cout << "floor( "<<a<<" ) = " << floor(a) << endl;
}

輸出

floor( 6.24 ) = 6

向上取整

ceil 函式用於返回給定雙精度整數的向上取整值。向上取整值表示四捨五入後的值。該函式接受一個雙精度值作為輸入,並返回使用 ceil() 計算的雙精度整數值。

double ceil( double)

呼叫語法

double x = ceil(5.24)

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   double a =6.64 ;
   cout << "ceil( "<<a<<" ) = " << ceil(a) << endl;
}

輸出

ceil( 6.64 ) = 7

絕對值

abs 函式返回整數的絕對值。該函式接受一個整數值並返回一個具有相同大小但符號為正的整數值。

double abs( double)

呼叫語法

double x = abs(-512)

示例

 線上演示

#include <iostream>
#include <math.h>
using namespace std;
int main(){
   int a = -345 ;
   cout << "abs( "<<a<<" ) = " << abs(a) << endl;
}

輸出

abs( -345 ) = 345

更新於: 2019年9月19日

3K+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.