NumPy floor(): Floor Function for Array Elements

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.

ParameterDescription
xInput array or numeric data-type
outOptional, ndarray to store the result; must match expected output shape
whereOptional, boolean array mask; elements where True are evaluated, False left untouched
dtypeOptional, data-type override for output

Basic floor() Examples 🔝

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.]

Legacy vs Modernized Code Comparison 🔝

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.

Using out Buffer 🔝

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.]

Using where Mask 🔝

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.  ]

Comparison with ceil() and trunc() 🔝

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

Common Pitfalls 🔝

  • Floating-Point Dtype Retention: np.floor() returns floating-point arrays even if inputs represent integers, preserving floating representation.
  • Negative Number Direction: Remember that floor rounds further away from zero for negative numbers (e.g., floor(-1.2) is -2.0).

Practice in Google Colab 🔝

Test floor() with arrays, preallocated buffers, boolean masks, and run verification test suites in our interactive Google Colab companion notebook:

Interactive Practice: Colab & GitHub

Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:

View on GitHub Open In Colab

Continue exploring related math functions: ceil(), round(), rad2deg(), and Math & ufuncs.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer