Skip to content

[Edit] NumPy: .ceil() #6989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions content/numpy/concepts/math-methods/terms/ceil/ceil.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Title: '.ceil()'
Description: 'Rounds each element in an array to the nearest greater integer.'
Description: 'Rounds each element in a NumPy array up to the nearest greater or equal integer.'
Subjects:
- 'Computer Science'
- 'Data Science'
Expand All @@ -19,17 +19,23 @@ In NumPy, the **`.ceil()`** [function](https://www.codecademy.com/resources/docs
## Syntax

```pseudo
numpy.ceil(input_value, out=None, where=True)
numpy.ceil(x, out=None, where=True)
```

- `input_value`: The input array or scalar that contains the elements to be rounded up.
**Parameters:**

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

## Example 1
**Return value:**

The `.ceil()` function returns a NumPy array with the smallest integers greater than or equal to each element in `x`, returned as floats.

## Example 1: Basic Usage of `.ceil()`

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

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

## Example 2
## Example 2: Using the `where` parameter in `.ceil()`

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:

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

## Codebyte Example
## Codebyte Example: Calculating Shipping Boxes

In this codebyte example, the `.ceil()` function rounds the elements in an array of floating-point numbers:
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:

```codebyte/python
# Importing the 'numpy' library as 'np'
import numpy as np

# Creating an array of floating-point numbers
arr = np.array([1.2, 2.5, -0.3, -1.9, 3.7])
# List of weights (in kg)
weights = np.array([9.5, 20.3, 14.7, 5.0, 0.9])

# Applying the '.ceil()' function
result = np.ceil(arr)
# Each box carries 10 kg
boxes_needed = np.ceil(weights / 10)

print(result)
print("Boxes required for each order:", boxes_needed)
```

## Frequently Asked Questions

### 1. What is the difference between `numpy.ceil()` and `numpy.floor()`?

- `numpy.ceil()` rounds up to the nearest higher integer.
- `numpy.floor()` rounds down to the nearest lower integer.

### 2. Does `numpy.ceil()` return integers?

No. Even though the result is a whole number, it is returned as a float.

### 3. Can `numpy.ceil()` be used with negative numbers?

Yes. For negative values, it still rounds up toward zero.

### 4. How is `numpy.ceil()` different from Python’s `math.ceil()`?

- `math.ceil()` works with a single float value and returns an int.
- `numpy.ceil()` works on arrays and returns a NumPy array of floats.