
數字程式庫 (numerics library) 提供各種數學相關的內容,有以下的標頭檔
| 名稱 | 功能 |
|---|---|
| <cmath> | 通用的數學函數。 |
| <complex> | 複數型態。 |
| <valarray> | 陣列的數值處理。 |
| <random>C++11 | 隨機數處理。 |
| <numeric> | 容器物件的數值處理。 |
| <ratio>C++11 | 編譯期間算術支援。 |
| <cfenv>C++11 | 浮點數環境存取函數。 |
舉一例如下
| 001 | #include <iostream> |
| 002 | #include <cmath> |
| 003 | |
| 004 | using namespace std; |
| 005 | |
| 006 | int main() { |
| 007 | cout << abs(-33.22) |
| 008 | << endl; |
| 009 | |
| 010 | cout << sqrt(16) |
| 011 | << endl; |
| 012 | |
| 013 | cout << pow(11, 3) |
| 014 | << endl; |
| 015 | |
| 016 | return 0; |
| 017 | } |
| 018 | |
| 019 | /* Kaiching Chang |
| 020 | u1505.cpp |
| 021 | 2014-02 */ |
此例示範 C++ 的 cmath ,因此要先 #include <cmath>
| 002 | #include <cmath> |
abs() 回傳參數的絕對值
| 007 | cout << abs(-33.22) |
sqrt() 回傳參數的平方根
| 010 | cout << sqrt(16) |
pow() 用來計算次方數,第一個參數為底數,第二個三數則是次方
| 013 | cout << pow(11, 3) |
編譯執行,結果如下
| $ g++ u1505.cpp |
| $ ./a.out |
| 33.22 |
| 4 |
| 1331 |
| $ |
continue ...
沒有留言:
張貼留言