import numpy as np
ar = np.array([1.57, 2.09, 2.79, -3.41, -3.62])
result = np.floor(ar)
print(result) # [ 1. 2. 2. -4. -4.]
numpy.floor(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) floor() is a universal function (ufunc) that returns an element-wise floor of the input, defined as the largest integer less than or equal to each element.The floor() function rounds numbers down toward negative infinity. For negative numbers, floor rounds further away from zero (e.g., -3.41 rounds down to -4.0). For upward rounding, see ceil(). For truncation towards zero, see round() or standard integer casting.
| Parameter | Description |
|---|---|
x | Input array or numeric data-type |
out | Optional, ndarray to store the result; must match expected output shape |
where | Optional, boolean array mask; elements where True are evaluated, False left untouched |
dtype | Optional, data-type override for output |
Passing an array of floating-point numbers to np.floor() returns the greatest lower integers:
import numpy as np
ar = np.array([1.57, 2.09, 2.79, -3.41, -3.62])
res = np.floor(ar)
print(res)
assert np.array_equal(res, np.array([1.0, 2.0, 2.0, -4.0, -4.0]))
Expected output
[ 1. 2. 2. -4. -4.]
The table below highlights how legacy scripts compare against the modernized, structured implementation:
| Aspect | Legacy Implementation | Modernized Implementation |
|---|---|---|
| Structure & Markup | Basic echo output with unformatted pre tags and missing metadata. | Structured HTML5 layout, Schema TechArticle markup, and responsive tables. |
| Code Presentation | Requires scrolling past introductory text to find code examples. | Core code snippet placed right at the top for instant visitor access. |
| Verification | Unchecked print statements with manual result inspection comments. | Automated unit test assertions (assert) verifying array outputs. |
| Ecosystem Integration | Isolated code blocks with sparse navigation links. | Comprehensive cross-linking, quick-jump navigation, and runnable Colab notebooks. |
Alternative output arrays can be preallocated using empty_like() to eliminate runtime memory allocation overhead in production loops:
ar = np.array([1.57, 2.09, 2.79, -3.41, -3.62])
ar_out = np.empty_like(ar)
np.floor(ar, out=ar_out)
print("Preallocated out buffer:", ar_out)
assert np.array_equal(ar_out, np.array([1.0, 2.0, 2.0, -4.0, -4.0]))
Expected output
Preallocated out buffer: [ 1. 2. 2. -4. -4.]
The where parameter allows selective execution of the floor function across boolean array masks:
ar = np.array([1.57, 2.09, 2.79, -3.41, -3.62])
ar_where = np.array([True, False, True, False, True])
masked_res = np.floor(ar, where=ar_where)
print("Masked floor result:", masked_res)
Expected output (Indices 1 and 3 are left un-evaluated)
Masked floor result: [ 1. 2.09 2. -3.41 -4. ]
| Function | Behavior | Example input 2.7 |
Example input -2.7 |
|---|---|---|---|
np.floor() |
Rounds down toward negative infinity | 2.0 |
-3.0 |
np.ceil() |
Rounds up toward positive infinity | 3.0 |
-2.0 |
np.trunc() |
Truncates fractional digits toward zero | 2.0 |
-2.0 |
np.floor() returns floating-point arrays even if inputs represent integers, preserving floating representation.Test floor() with arrays, preallocated buffers, boolean masks, and run verification test suites in our interactive Google Colab companion notebook:
Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:
View on GitHubContinue exploring related math functions: ceil(), round(), rad2deg(), and Math & ufuncs.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.