Programing/C++
[C++ 프로그래밍] 수학 함수
유니얼
2024. 7. 7. 23:06
728x90
C++ 기초 프로그래밍: 수학 함수
C++ 프로그래밍에서 수학 함수는 복잡한 수학 계산을 수행하는 데 매우 유용합니다. 이러한 함수들은 주로 <cmath> 헤더 파일에 정의되어 있으며, 이를 통해 다양한 수학 연산을 간단하게 구현할 수 있습니다. 이번 블로그 글에서는 C++에서 자주 사용하는 수학 함수들에 대해 알아보고, 각 함수의 사용 예제를 살펴보겠습니다.
<cmath> 헤더 파일
<cmath> 헤더 파일은 C++에서 수학 함수를 제공하는 라이브러리입니다. 이 라이브러리에는 다양한 수학 함수들이 포함되어 있어 복잡한 수학 계산을 손쉽게 수행할 수 있습니다.
주요 수학 함수
- std::floor 함수: 소수점 이하를 내림하여 가장 가까운 정수로 반환합니다.
- std::ceil 함수: 소수점 이하를 올림하여 가장 가까운 정수로 반환합니다.
- std::abs 함수: 절대값을 반환합니다.
- std::exp 함수: 자연상수 e의 거듭제곱을 계산합니다.
- std::pow 함수: x의 y제곱을 계산합니다.
- std::log 함수: 자연로그를 계산합니다.
- std::log10 함수: 로그(base 10)를 계산합니다.
- std::sqrt 함수: 제곱근을 계산합니다.
- std::round 함수: 반올림하여 가장 가까운 정수로 반환합니다.
예제 코드
다음 예제 코드는 위에서 언급한 다양한 수학 함수를 사용하는 방법을 보여줍니다:
#include <iostream>
#include <cmath>
int main() {
// 변수 선언
double weight { 7.7 };
double savings {-5000 };
// floor 함수: 소수점 이하를 내림
std::cout << "Weight rounded to floor is : " << std::floor(weight) << std::endl;
// ceil 함수: 소수점 이하를 올림
std::cout << "Weight rounded to ceil is : " << std::ceil(weight) << std::endl;
// abs 함수: 절대값 반환
std::cout << "Abs of weight is : " << std::abs(weight) << std::endl;
std::cout << "Abs of savings is : " << std::abs(savings) << std::endl;
// exp 함수: e^x 계산, 여기서 e는 자연상수 2.71828
double exponential = std::exp(10);
std::cout << "The exponential of 10 is : " << exponential << std::endl;
// pow 함수: x^y 계산
std::cout << "3 ^ 4 is : " << std::pow(3, 4) << std::endl;
std::cout << "9 ^ 3 is : " << std::pow(9, 3) << std::endl;
// log 함수: 자연로그 계산 (기본 베이스 e)
std::cout << "Log; to get 54.59, you would elevate e to the power of : "
<< std::log(54.59) << std::endl;
// log10 함수: 로그(base 10) 계산
std::cout << "To get 10000, you'd need to elevate 10 to the power of : "
<< std::log10(10000) << std::endl; // 4
// sqrt 함수: 제곱근 계산
std::cout << "The square root of 81 is : " << std::sqrt(81) << std::endl;
// round 함수: 반올림, 0에서 멀어지는 방향으로 반올림
std::cout << "3.654 rounded to : " << std::round(3.654) << std::endl;
std::cout << "2.5 is rounded to : " << std::round(2.5) << std::endl;
std::cout << "2.4 is rounded to : " << std::round(2.4) << std::endl;
return 0;
}
출력 결과
위 코드를 실행하면 다음과 같은 결과를 얻을 수 있습니다:
Weight rounded to floor is : 7
Weight rounded to ceil is : 8
Abs of weight is : 7.7
Abs of savings is : 5000
The exponential of 10 is : 22026.5
3 ^ 4 is : 81
9 ^ 3 is : 729
Log; to get 54.59, you would elevate e to the power of : 4
To get 10000, you'd need to elevate 10 to the power of : 4
The square root of 81 is : 9
3.654 rounded to : 4
2.5 is rounded to : 3
2.4 is rounded to : 2
주요 함수 설명
std::floor 함수:
std::cout << "Weight rounded to floor is : " << std::floor(weight) << std::endl;
- 소수점 이하를 내림하여 가장 가까운 정수로 반환합니다.
std::ceil 함수:
std::cout << "Weight rounded to ceil is : " << std::ceil(weight) << std::endl;
- 소수점 이하를 올림하여 가장 가까운 정수로 반환합니다.
std::abs 함수:
std::cout << "Abs of weight is : " << std::abs(weight) << std::endl;
std::cout << "Abs of savings is : " << std::abs(savings) << std::endl;
- 절대값을 반환합니다.
std::exp 함수:
double exponential = std::exp(10);
std::cout << "The exponential of 10 is : " << exponential << std::endl;
- 자연상수 e의 거듭제곱을 계산합니다.
std::pow 함수:
std::cout << "3 ^ 4 is : " << std::pow(3, 4) << std::endl;
std::cout << "9 ^ 3 is : " << std::pow(9, 3) << std::endl;
- x의 y제곱을 계산합니다.
std::log 함수:
std::cout << "Log; to get 54.59, you would elevate e to the power of : " << std::log(54.59) << std::endl;
- 자연로그(베이스 e)를 계산합니다.
std::log10 함수:
std::cout << "To get 10000, you'd need to elevate 10 to the power of : " << std::log10(10000) << std::endl;
- 로그(베이스 10)를 계산합니다.
std::sqrt 함수:
std::cout << "The square root of 81 is : " << std::sqrt(81) << std::endl;
- 제곱근을 계산합니다.
std::round 함수:
std::cout << "3.654 rounded to : " << std::round(3.654) << std::endl;
- 반올림하여 가장 가까운 정수로 반환합니다.
결론
C++의 <cmath> 헤더 파일은 다양한 수학 함수를 제공하여 복잡한 수학 계산을 손쉽게 수행할 수 있습니다.
반응형