9. cmath

han811·2021년 2월 20일
0

c++

목록 보기
9/14
post-thumbnail

cmath

1. pow
#include<iostream>
#include<cmath>
using namespace std;
 
int main(void)
{
    cout << "> C++" << endl;
    cout << "10 ^ 2 = " << pow(10, 2) << endl;
    cout << "2.2 ^ 3.3 = " << pow(2.2, 3.3) << endl;
 
    return 0;
}
  • 위와 같이 특정 수의 제곱승을 구하는데 사용됩니다.
2. sqrt
#include<iostream>
#include<cmath>
using namespace std;
 
int main(void)
{
    double a = 9;
    double b = 85.4;

    cout << "> C++" << endl;
    cout << "sqrt(9) = " << sqrt(a) << endl;
    cout << "sqrt(85.4) = " << sqrt(b) << endl;
 
    return 0;
}
  • 위와 같이 특정 수의 제곱근을 구하는데 사용됩니다.
3. abs
#include<iostream>
#include<cmath>    //abs for float, double
#include<cstdlib> //abs for int, long int
using namespace std;
int main(void)
{
    int a1 = -1111;
    int a2 = 1000;
    float b1 = -22.22f;
    float b2 = 20.22f;
 
    cout << "[C++ abs() example]" << endl;
    cout << "abs(- int) : " << abs(a1) << endl;
    cout << "abs(+ int) : " << abs(a2) << endl;
    cout << "abs(- float) : " << abs(b1) << endl;
    cout << "abs(+ float) : " << abs(b2) << endl;
 
    cout << endl;
    return 0;
}
  • 위와 같이 절대값은 cmath의 std::abs를 사용하면 구할 수 있습니다.

Reference

profile
han811

0개의 댓글