• 티스토리 홈
  • 프로필사진
    유니얼
  • 방명록
  • 공지사항
  • 태그
  • 블로그 관리
  • 글 작성
유니얼
  • 프로필사진
    유니얼
    • 분류 전체보기 (295)
      • Unity (17)
        • 게임 개발 (5)
      • Unreal (24)
        • 게임 개발 (20)
      • DirectX (36)
      • 코딩테스트 (91)
        • 프로그래머스 (25)
        • 백준 (66)
      • Google Workspace (1)
      • Programing (102)
        • C# (68)
        • C++ (24)
        • JavaScript (10)
      • 게임 서버 프로그래밍 (17)
      • Web (6)
        • 슈퍼코딩 (6)
  • 방문자 수
    • 전체:
    • 오늘:
    • 어제:
  • 최근 댓글
    등록된 댓글이 없습니다.
  • 최근 공지
    등록된 공지가 없습니다.
# Home
# 공지사항
#
# 태그
# 검색결과
# 방명록
  • [C++ 프로그래밍] 수학 함수
    2024년 07월 07일
    • 유니얼
    • 작성자
    • 2024.07.07.:06
    728x90

    C++ 기초 프로그래밍: 수학 함수

    C++ 프로그래밍에서 수학 함수는 복잡한 수학 계산을 수행하는 데 매우 유용합니다. 이러한 함수들은 주로 <cmath> 헤더 파일에 정의되어 있으며, 이를 통해 다양한 수학 연산을 간단하게 구현할 수 있습니다. 이번 블로그 글에서는 C++에서 자주 사용하는 수학 함수들에 대해 알아보고, 각 함수의 사용 예제를 살펴보겠습니다.

    <cmath> 헤더 파일

    <cmath> 헤더 파일은 C++에서 수학 함수를 제공하는 라이브러리입니다. 이 라이브러리에는 다양한 수학 함수들이 포함되어 있어 복잡한 수학 계산을 손쉽게 수행할 수 있습니다.

    주요 수학 함수

    1. std::floor 함수: 소수점 이하를 내림하여 가장 가까운 정수로 반환합니다.
    2. std::ceil 함수: 소수점 이하를 올림하여 가장 가까운 정수로 반환합니다.
    3. std::abs 함수: 절대값을 반환합니다.
    4. std::exp 함수: 자연상수 e의 거듭제곱을 계산합니다.
    5. std::pow 함수: x의 y제곱을 계산합니다.
    6. std::log 함수: 자연로그를 계산합니다.
    7. std::log10 함수: 로그(base 10)를 계산합니다.
    8. std::sqrt 함수: 제곱근을 계산합니다.
    9. 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> 헤더 파일은 다양한 수학 함수를 제공하여 복잡한 수학 계산을 손쉽게 수행할 수 있습니다.

    반응형
    다음글
    다음 글이 없습니다.
    이전글
    이전 글이 없습니다.
    댓글
조회된 결과가 없습니다.
스킨 업데이트 안내
현재 이용하고 계신 스킨의 버전보다 더 높은 최신 버전이 감지 되었습니다. 최신버전 스킨 파일을 다운로드 받을 수 있는 페이지로 이동하시겠습니까?
("아니오" 를 선택할 시 30일 동안 최신 버전이 감지되어도 모달 창이 표시되지 않습니다.)
목차
표시할 목차가 없습니다.
    • 안녕하세요
    • 감사해요
    • 잘있어요

    티스토리툴바