In mathematics and computer science, the floor and ceiling functions map a real number to the greatest preceding or the least succeeding integer, respectively.
floor(x) : Returns the largest integer that is smaller than or equal to x (i.e : rounds downs the nearest integer).
// Here x is the floating point value. // Returns the largest integer smaller // than or equal to x double floor(double x)
Examples of Floor:
Input : 2.5 Output : 2 Input : -2.1 Output : -3 Input : 2.9 Output : 2
Floor is : 2 Floor is : -3
ceil(x) : Returns the smallest integer that is greater than or equal to x (i.e : rounds up the nearest integer).
// Here x is the floating point value. // Returns the smallest integer greater // than or equal to x double ceiling(double x)
Examples of Ceil:
Input : 2.5 Output : 3 Input : -2.1 Output : -2 Input : 2.9 Output : 3
Ceil is : 3 Ceil is : -2
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- Finding Floor and Ceil of a Sorted Array using C++ STL
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 1 (Numeric Functions)
- Maximum number of tiles required to cover the floor of given size using 2x1 size tiles
- fill() and fill_n() functions in C++ STL
- Forward List in C++ | Set 1 (Introduction and Important Functions)
- strtok() and strtok_r() functions in C with examples
- strdup() and strndup() functions in C/C++
- Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
- Pure Virtual Functions and Abstract Classes in C++
- Python | Set 2 (Variables, Expressions, Conditions and Functions)
- Wide char and library functions in C++
- Find and print duplicate words in std::vector<string> using STL functions
- Explicitly Defaulted and Deleted Functions in C++ 11
- beta(), betaf() and betal() functions in C++ STL
- std::legendre, std::legendref and std::legendrel functions in C++17
- Array in Python | Set 1 (Introduction and Functions)
- Virtual Functions and Runtime Polymorphism in C++ | Set 1 (Introduction)
- asin() and atan() functions in C/C++ with Example