별의 공부 블로그 🧑🏻‍💻
728x90
728x170

*[STL] ceil, floor, round

 

- 헤더 파일 (header)

C : <math.h>

C++ : <cmath>

- ceil()  : 올림 함수 (C90, C99, C++98, C++11)

- floor() : 내림 함수 (C90, C99, C++98, C++11)

- round() : 반올림 함수(C99, C++11).

               floor(x + 0.5)로 구현할 수 있음.

- 자세한 내용

ceil : http://www.cplusplus.com/reference/cmath/ceil/

floor : http://www.cplusplus.com/reference/cmath/floor/?kw=floor

round : http://www.cplusplus.com/reference/cmath/round/?kw=round

 

[예제 코드]

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cmath>
 
using namespace std;                                                                                                             
 
int main()
{
    double a1 = 3.5;
    double a2 = 4.2;
    double a3 = 6.2;
    double a4 = 4.7;
    double a5 = 2.2;
    double a6 = 1.7;
    double a7 = -5.4;
    double a8 = -7.2;
    double a9 = -4.6;
    double a10 = -3.8
    
    /* 양수 */ 
 
    cout << ceil(a1) << endl;          // 올림 (4)
    cout << ceil(a2) << endl;          // 올림 (5)
    cout << floor(a3) << endl;         // 내림 (6)
    cout << floor(a4) << endl;         // 내림 (4)
    cout << round(a5) << endl;         // 반올림 (2)
    cout << round(a6) << endl;         // 반올림 (2)
    
    /* floor을 이용하여 반올림 구현 하기 */
    // 반올림 
    cout << floor(a5 + 0.5<< endl;   // 반올림 (2)
    cout << floor(a6 + 0.5<< endl;   // 반올림 (2)  
 
    /* 음수 */
 
    cout << ceil(a7) << endl;          // 올림 (-5)
    cout << ceil(a8) << endl;          // 올림 (-7)
    cout << floor(a9) << endl;         // 내림 (-5)
    cout << floor(a10) << endl;        // 내림 (-4)
    cout << round(a7) << endl;         // 반올림 (-5)
    cout << round(a9) << endl;         // 반올림 (-5)
    cout << floor(a7 + 0.5<< endl;   // 반올림 (-5)
    cout << floor(a9 + 0.5<< endl;   // 반올림 (-5)
    
    return 0;    
}
cs
728x90
그리드형(광고전용)

'Programming > C++' 카테고리의 다른 글

[STL] binary_search  (0) 2017.11.08
[STL] bitset  (0) 2017.10.26
[STL] sprintf  (0) 2017.10.21
[STL] getline  (0) 2017.10.20
[STL] sort  (0) 2017.09.22
C++ Character Literals  (0) 2017.09.02
인수와 레퍼런스  (0) 2017.05.30
함수 템플릿 (Function Template)  (0) 2017.05.29
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️
starrykss
starrykss
별의 공부 블로그 🧑🏻‍💻


📖 Contents 📖