【C++ 筆記】數學函式庫(cmath library) - part 7.5

很感謝你點進來這篇文章。

你好,我並不是什麼 C++、程式語言的專家,所以本文若有些錯誤麻煩請各位鞭大力一點,我極需各位的指正及指導!!本系列文章的性質主要以詼諧的口吻,一派輕鬆的態度自學程式語言,如果你喜歡,麻煩留言說聲文章讚讚吧!

上節介紹到函數的概念,我們說函數就像是一台有功能的機器,在數學函式庫中,裡面的函數恰好能展現此一概念,故此節介紹 cmath。

cmath

語法:

1
#include <cmath>

有關其函數,列下表:

序號函數
1double cos(double);
2double sin(double);
3double tan(double);
4double log(double);
5double pow(double, double);
6double hypot(double, double); -> 註:畢氏定理計算斜邊公式(根號a平方+b平方)
7double sqrt(double); -> 計算平方根
8int abs(int); -> 計算絕對值
9double fabs(double); -> 回傳任意一個浮點數的絕對值
10double floor(double); -> 小數點無條件退位至整數位

註:ceil(x) 函數與 floor(x) 相對,為無條件進位。(正確定義為:Returns the value of x rounded up to its nearest integer,為了方便理解所以故作此解釋)

有關細則可至:C++ cmath Library Reference (cmath functions) w3schools 查看。

:::success
Returns the value of x rounded up to its nearest integer
回傳 x 的值進位到最接近的整數
:::

表格來源:https://www.runoob.com/cplusplus/cpp-numbers.html

以下是一個練習範例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cmath> // 引入數學函數標頭檔

int main() {
// 定義常數
const double x = 2.5;
const double y = 3.0;

// 輸出常數值
std::cout << "常數 x = " << x << std::endl;
std::cout << "常數 y = " << y << std::endl;

// 三角函數
std::cout << "cos(x) = " << cos(x) << std::endl;
std::cout << "sin(x) = " << sin(x) << std::endl;
std::cout << "tan(x) = " << tan(x) << std::endl;

// 指數與對數
std::cout << "log(x) = " << log(x) << " (自然對數)" << std::endl;
std::cout << "pow(x, y) = " << pow(x, y) << " (x 的 y 次方)" << std::endl;

// 幾何函數
std::cout << "hypot(x, y) = " << hypot(x, y) << " (x 和 y 的歐幾里德距離)" << std::endl;

// 平方根
std::cout << "sqrt(x) = " << sqrt(x) << std::endl;

// 絕對值
std::cout << "abs(-x) = " << abs(-x) << " (整數絕對值)" << std::endl;
std::cout << "fabs(-x) = " << fabs(-x) << " (浮點數絕對值)" << std::endl;

// 無條件進退位
std::cout << "floor(x) = " << floor(x) << " (無條件退位)" << std::endl;
std::cout << "ceil(x) = " << ceil(x) << " (無條件進位)" << std::endl;

return 0;
}

輸出結果:

1
2
3
4
5
6
7
8
9
10
11
12
13
常數 x = 2.5
常數 y = 3.0
cos(x) = -0.801144
sin(x) = 0.598472
tan(x) = -0.747022
log(x) = 0.916291 (自然對數)
pow(x, y) = 15.625 (x 的 y 次方)
hypot(x, y) = 3.90512 (x 和 y 的歐幾里德距離)
sqrt(x) = 1.58114
abs(-x) = 2 (整數絕對值)
fabs(-x) = 2.5 (浮點數絕對值)
floor(x) = 2 (無條件退位)
ceil(x) = 3 (無條件進位)

註:歐幾里德距離就是高中學的距離公式,通常也使用勾股定理(畢氏定理)作為歐幾里德距離,此例中即使用勾股定理。

隨機數(random)

要生成隨機數,需用到兩種函數:rand()、srand()。

rand() 用於生成偽隨機數,在之前需要用到 srand(),為什麼呢?各位可以先想想再點開以下答案。

:::spoiler 點我看答案
srand() 用於設定隨機種子(randomseed),若不使用 srand() 當作 seed,則 rand() 預設的 seed 為 0,意即此為固定的隨機序列,每次打開程式時都是固定的隨機數。

設定隨機種子會讓隨機序列更加隨機。
:::

以下是一個範例,用 time() 函數得到系統時間的秒數,當作隨機種子使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main ()
{
int i,j;

// 設定隨機種子
srand( (unsigned)time( NULL ) );

/* 生成 10 個隨機數 */
for( i = 0; i < 10; i++ )
{
// 生成實際的隨機數
j= rand();
cout <<"random: " << j << endl;
}

return 0;
}

來源:https://www.runoob.com/cplusplus/cpp-numbers.html

輸出結果(不定):

1
2
3
4
5
6
7
8
9
10
random: 1711621864
random: 1916654369
random: 1613611712
random: 1966213583
random: 193755691
random: 1786444714
random: 149637669
random: 1314185198
random: 2077363357
random: 881636108

參考資料

C++ cmath Library Reference (cmath functions)

C++ 数字 | 菜鸟教程