You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/numpy/concepts/math-methods/terms/ceil/ceil.md
+38-13Lines changed: 38 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
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.'
4
4
Subjects:
5
5
- 'Computer Science'
6
6
- 'Data Science'
@@ -19,17 +19,23 @@ In NumPy, the **`.ceil()`** [function](https://www.codecademy.com/resources/docs
19
19
## Syntax
20
20
21
21
```pseudo
22
-
numpy.ceil(input_value, out=None, where=True)
22
+
numpy.ceil(x, out=None, where=True)
23
23
```
24
24
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.
26
28
-`out` (Optional): A location where the result will be stored. If no value is provided, a new array is returned.
27
29
-`where` (Optional): A boolean array that specifies the condition for rounding.
28
30
- If the condition is `True` at a given position, the element will be rounded.
29
31
- If the condition is `False`, the original value is retained.
30
32
- If no value is provided, the `.ceil()` function is applied to all elements.
31
33
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()`
33
39
34
40
This example demonstrates using the `.ceil()` function to round an array of floating-point numbers up to the nearest greater integer:
35
41
@@ -52,7 +58,7 @@ The above code results in the following output:
52
58
[ 2. 3. -0. -3.]
53
59
```
54
60
55
-
## Example 2
61
+
## Example 2: Using the `where` parameter in `.ceil()`
56
62
57
63
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:
58
64
@@ -75,19 +81,38 @@ The above code results in the following output:
75
81
[ 2. 3. -0.2 -3.1]
76
82
```
77
83
78
-
## Codebyte Example
84
+
## Codebyte Example: Calculating Shipping Boxes
79
85
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:
81
87
82
88
```codebyte/python
83
-
# Importing the 'numpy' library as 'np'
84
89
import numpy as np
85
90
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])
88
93
89
-
# Applying the '.ceil()' function
90
-
result = np.ceil(arr)
94
+
# Each box carries 10 kg
95
+
boxes_needed = np.ceil(weights / 10)
91
96
92
-
print(result)
97
+
print("Boxes required for each order:", boxes_needed)
93
98
```
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