溫馨提示×

C++中math庫的用法是什么

c++
小億
141
2024-06-04 09:26:22
欄目: 編程語言

C++中的math庫提供了一些數學函數和常量,可以用來進行數學運算和計算。使用math庫需要包含頭文件。

以下是一些常用的math庫函數和常量:

  1. 數學函數:
  • sqrt(x):計算x的平方根
  • pow(x, y):計算x的y次冪
  • sin(x)、cos(x)、tan(x):計算x的正弦、余弦、正切值
  • log(x)、log10(x):計算x的自然對數和以10為底的對數
  • exp(x):計算e的x次冪
  • ceil(x)、floor(x):對x進行向上取整和向下取整
  1. 常量:
  • M_PI:圓周率π的值
  • M_E:自然對數的底e的值

示例代碼:

#include <iostream>
#include <cmath>

int main() {
    double x = 2.5;
    
    std::cout << "Square root of " << x << " is " << sqrt(x) << std::endl;
    std::cout << "2 to the power of 3 is " << pow(2, 3) << std::endl;
    std::cout << "sin(PI/2) is " << sin(M_PI/2) << std::endl;
    
    return 0;
}

以上是math庫的基本使用方法,更多詳細的函數和常量可以查閱C++標準庫的文檔。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女