0
0
NumPydata~10 mins

np.round(), np.floor(), np.ceil() in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.round(), np.floor(), np.ceil()
Start with array of floats
Apply chosen function element-wise
Get new array with transformed values
End
Start with a float array, pick one of the three functions, apply it to each element, and get a new array with rounded, floored, or ceiled values.
Execution Sample
NumPy
import numpy as np
arr = np.array([1.2, 2.5, 3.7, -1.3])
rounded = np.round(arr)
floored = np.floor(arr)
ceiled = np.ceil(arr)
This code creates an array and applies np.round, np.floor, and np.ceil to it, producing three new arrays.
Execution Table
StepInput Valuenp.round()np.floor()np.ceil()
11.21.01.02.0
22.52.02.03.0
33.74.03.04.0
4-1.3-1.0-2.0-1.0
5End of array---
💡 All elements processed; arrays with rounded, floored, and ceiled values are complete.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
arr[1.2, 2.5, 3.7, -1.3][1.2, 2.5, 3.7, -1.3][1.2, 2.5, 3.7, -1.3][1.2, 2.5, 3.7, -1.3][1.2, 2.5, 3.7, -1.3][1.2, 2.5, 3.7, -1.3]
rounded[][1.0][1.0, 2.0][1.0, 2.0, 4.0][1.0, 2.0, 4.0, -1.0][1.0, 2.0, 4.0, -1.0]
floored[][1.0][1.0, 2.0][1.0, 2.0, 3.0][1.0, 2.0, 3.0, -2.0][1.0, 2.0, 3.0, -2.0]
ceiled[][2.0][2.0, 3.0][2.0, 3.0, 4.0][2.0, 3.0, 4.0, -1.0][2.0, 3.0, 4.0, -1.0]
Key Moments - 3 Insights
Why does np.round(2.5) become 2.0 instead of 3.0?
np.round uses 'bankers rounding' which rounds .5 to the nearest even number. See execution_table row 2 where 2.5 rounds to 2.0.
Why is np.floor(-1.3) equal to -2.0, not -1.0?
np.floor always rounds down to the next lower integer. For negative numbers, 'down' means more negative. See execution_table row 4.
What is the difference between np.ceil and np.round for positive numbers?
np.ceil always rounds up to the next integer, while np.round rounds to the nearest integer. Compare rows 1-3 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the np.round() value for input 3.7 at step 3?
A4.0
B3.0
C3.7
D2.0
💡 Hint
Check the third row under the np.round() column in execution_table.
At which step does np.floor() output become -2.0?
AStep 1
BStep 4
CStep 2
DStep 3
💡 Hint
Look at the np.floor() column for negative inputs in execution_table.
If the input array had 2.6 instead of 2.5, what would np.round(2.6) be?
A2.0
B2.5
C3.0
D4.0
💡 Hint
np.round rounds to nearest integer; check how 3.7 was rounded in execution_table.
Concept Snapshot
np.round(array): rounds each element to nearest integer (bankers rounding for .5)
np.floor(array): rounds each element down to nearest lower integer
np.ceil(array): rounds each element up to nearest higher integer
All return new arrays without changing original
Useful for controlling decimal precision or integer conversion
Full Transcript
We start with an array of floating-point numbers. We apply three numpy functions: np.round, np.floor, and np.ceil. np.round rounds each number to the nearest integer, using bankers rounding where .5 rounds to the nearest even number. np.floor rounds each number down to the next lower integer, which means for negative numbers it goes to a more negative integer. np.ceil rounds each number up to the next higher integer. We see step-by-step how each input value transforms under these functions, building new arrays. This helps us understand how these rounding functions behave differently on positive and negative numbers.