본문 바로가기

Programming

C++ Math - floor/ceil/round (내림/올림/반올림)

C++ Math - floor/ceil/round (내림/올림/반올림)



 자바 Math class 관련 포스팅을 하면서 C++도 같이 한다.

C++ 은 MSDN에 자세하게 잘 나와있어서 거의 고대로 가져오고 반올림만 추가했다.



math.h 헤더 추가해주고,


floor    -> 내림    (floor)

ceil      -> 올림    (ceiling)

round   -> 반올림 (round)



 The thing different with Java Math class is that the math in C++ about floor, round, and ceil is not only for double data type, but also include float.



 자바의 Math 클래스와 다른 점은, 자바에서는 abs (절대값) 빼고는 전부 double 자료형만 취급했지만, C++에서는 double, float 두개가 따로따로 되어있다.



============================================================


#include <math.h>  

#include <stdio.h>  


int main(void)

{

double y;


y = floor(2.8);

printf("The floor of 2.8 is %f\n", y);

y = floor(-2.8);

printf("The floor of -2.8 is %f\n", y);


y = round(2.4);

printf("The round of 2.4 is %f\n", y);

y = round(-2.8);

printf("The round of -2.8 is %f\n", y);


y = ceil(2.8);

printf("The ceil of 2.8 is %f\n", y);

y = ceil(-2.8);

printf("The ceil of -2.8 is %f\n", y);


}


Result-


The floor of 2.8 is 2.000000

The floor of -2.8 is -3.000000

The ceil of 2.8 is 3.000000

The ceil of -2.8 is -2.000000

The round of 2.4 is 2.000000

The round of -2.8 is -3.000000


===========================================================



자바 Math 클래스는 아래서 확인 가능



2018/09/27 - [Programming/Java] - Java 자바 Math class - round/ceil/floor/abs (반올림/올림/내림/절대값)



2019/02/12 - [Programming/C++] - How to use map in c++ STL, c++ 맵 STL 사용방법/초기화/예제, java, example, sample, library, find, insert, first

2019/02/04 - [Programming/C++] - How to add DLL / external lib file to VisualStudio, 비주얼스튜디오 외부라이브러리 dll 파일 추가 방법, example, c++ java api

2018/12/26 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍


2018/12/21 - [Programming/Programming Language] - Scala Advantages & Disadvantages 스칼라 장단점, 객체지향, 함수적, 절차지향적, 프로그래밍언어


2018/11/28 - [Programming/Image Processing] - OpenCV How to get min/max value from matrix, 매트릭스 최소값/최대값. Java,c++,cpp,imageprocessing



2019/02/14 - [Algorithm/Leet Code] - LeetCode #283 MoveZeroes. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/02/13 - [Algorithm/Leet Code] - LeetCode #258 AddDigits. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/02/12 - [Algorithm/Leet Code] - LeetCode #136 SingleNumber. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/02/09 - [Algorithm/Leet Code] - LeetCode #709 ToLowerCase. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/02/08 - [Algorithm/Leet Code] - LeetCode #400 NthDigit. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접

2019/02/03 - [Algorithm/Leet Code] - LeetCode #393 UTF-8 Validation. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접



2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #60 sudoku. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #59 spiralNumbers. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/08 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #58 messageFromBinaryCode. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/07 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #57 fileNaming. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive