Skip to content

Commit b281e3c

Browse files
[Edit] NumPy: .ceil() (#6989)
* [Edit] SQL: DATEDIFF() * Update datediff.md * [Edit] NumPy: `.ceil()` ---------
1 parent e08a196 commit b281e3c

File tree

1 file changed

+38
-13
lines changed
  • content/numpy/concepts/math-methods/terms/ceil

1 file changed

+38
-13
lines changed

content/numpy/concepts/math-methods/terms/ceil/ceil.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Title: '.ceil()'
3-
Description: 'Rounds each element in an array to the nearest greater integer.'
3+
Description: 'Rounds each element in a NumPy array up to the nearest greater or equal integer.'
44
Subjects:
55
- 'Computer Science'
66
- 'Data Science'
@@ -19,17 +19,23 @@ In NumPy, the **`.ceil()`** [function](https://www.codecademy.com/resources/docs
1919
## Syntax
2020

2121
```pseudo
22-
numpy.ceil(input_value, out=None, where=True)
22+
numpy.ceil(x, out=None, where=True)
2323
```
2424

25-
- `input_value`: The input array or scalar that contains the elements to be rounded up.
25+
**Parameters:**
26+
27+
- `x`: The input array or scalar that contains the elements to be rounded up.
2628
- `out` (Optional): A location where the result will be stored. If no value is provided, a new array is returned.
2729
- `where` (Optional): A boolean array that specifies the condition for rounding.
2830
- If the condition is `True` at a given position, the element will be rounded.
2931
- If the condition is `False`, the original value is retained.
3032
- If no value is provided, the `.ceil()` function is applied to all elements.
3133

32-
## Example 1
34+
**Return value:**
35+
36+
The `.ceil()` function returns a NumPy array with the smallest integers greater than or equal to each element in `x`, returned as floats.
37+
38+
## Example 1: Basic Usage of `.ceil()`
3339

3440
This example demonstrates using the `.ceil()` function to round an array of floating-point numbers up to the nearest greater integer:
3541

@@ -52,7 +58,7 @@ The above code results in the following output:
5258
[ 2. 3. -0. -3.]
5359
```
5460

55-
## Example 2
61+
## Example 2: Using the `where` parameter in `.ceil()`
5662

5763
This example shows how the `where` parameter of `.ceil()` can be used to apply rounding only to certain elements of the array. In this case, only the positive numbers are rounded:
5864

@@ -75,19 +81,38 @@ The above code results in the following output:
7581
[ 2. 3. -0.2 -3.1]
7682
```
7783

78-
## Codebyte Example
84+
## Codebyte Example: Calculating Shipping Boxes
7985

80-
In this codebyte example, the `.ceil()` function rounds the elements in an array of floating-point numbers:
86+
Suppose there is a list of item weights, and each shipping box can carry up to 10 kg. The following example uses `numpy.ceil()` to calculate how many boxes will be needed for each order:
8187

8288
```codebyte/python
83-
# Importing the 'numpy' library as 'np'
8489
import numpy as np
8590
86-
# Creating an array of floating-point numbers
87-
arr = np.array([1.2, 2.5, -0.3, -1.9, 3.7])
91+
# List of weights (in kg)
92+
weights = np.array([9.5, 20.3, 14.7, 5.0, 0.9])
8893
89-
# Applying the '.ceil()' function
90-
result = np.ceil(arr)
94+
# Each box carries 10 kg
95+
boxes_needed = np.ceil(weights / 10)
9196
92-
print(result)
97+
print("Boxes required for each order:", boxes_needed)
9398
```
99+
100+
## Frequently Asked Questions
101+
102+
### 1. What is the difference between `numpy.ceil()` and `numpy.floor()`?
103+
104+
- `numpy.ceil()` rounds up to the nearest higher integer.
105+
- `numpy.floor()` rounds down to the nearest lower integer.
106+
107+
### 2. Does `numpy.ceil()` return integers?
108+
109+
No. Even though the result is a whole number, it is returned as a float.
110+
111+
### 3. Can `numpy.ceil()` be used with negative numbers?
112+
113+
Yes. For negative values, it still rounds up toward zero.
114+
115+
### 4. How is `numpy.ceil()` different from Python’s `math.ceil()`?
116+
117+
- `math.ceil()` works with a single float value and returns an int.
118+
- `numpy.ceil()` works on arrays and returns a NumPy array of floats.

0 commit comments

Comments
 (0)