Ref Math Ceil
## Python math.ceil() Method
The `math.ceil()` method is a built-in function in Python's standard `math` module. It rounds a number **up** to the nearest integer.
The term "ceil" stands for "ceiling", which represents the smallest integer greater than or equal to the given number.
---
### Syntax
To use the `math.ceil()` method, you must first import the `math` module:
```python
import math
math.ceil(x)
```
### Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `x` | Numeric (`float` or `int`) | **Required.** The number you want to round up. |
*Note: If `x` is not a number (e.g., a string or a list), the method will raise a `TypeError`.*
### Return Value
* **Type:** `int` (Integer)
* **Description:** Returns the smallest integer greater than or equal to `x`.
---
### Code Examples
The following example demonstrates how `math.ceil()` rounds different types of numbers (positive, negative, and whole numbers) up to the nearest integer:
```python
# Import the math module
import math
# Output the ceiling of various numbers
print(math.ceil(1.4)) # Output: 2
print(math.ceil(5.3)) # Output: 6
print(math.ceil(-5.3)) # Output: -5 (Note: -5 is greater than -5.3)
print(math.ceil(22.6)) # Output: 23
print(math.ceil(10.0)) # Output: 10 (Already an integer, returns 10)
```
**Output:**
```text
2
6
-5
23
10
```
---
### Key Considerations & Comparisons
#### 1. Behavior with Negative Numbers
When dealing with negative numbers, remember that "rounding up" means moving towards positive infinity (to the right on the number line).
* For example, `math.ceil(-5.3)` returns `-5` because `-5` is greater than `-5.3`.
#### 2. `math.ceil()` vs `math.floor()`
* **`math.ceil(x)`**: Rounds **up** to the nearest integer (towards positive infinity).
* **`math.floor(x)`**: Rounds **down** to the nearest integer (towards negative infinity).
```python
import math
print(math.ceil(3.2)) # Output: 4
print(math.floor(3.2)) # Output: 3
print(math.ceil(-3.2)) # Output: -3
print(math.floor(-3.2)) # Output: -4
```
YouTip