NumPy - Rounding Functions
NumPy Rounding Functions
Rounding functions in NumPy are used to round off the values in arrays to a specified number of decimal places. These functions are helpful in various scenarios, such as when you need to present values in a cleaner format or when performing numerical computations where precision control is necessary.
NumPy provides several rounding functions, including round(), floor(), ceil(), and trunc(), each serving a different purpose when it comes to rounding values.
Rounding Using round() Function
The numpy.round() function rounds each element in the input array to the specified number of decimal places. If no number of decimals is provided, it rounds to the nearest integer.
round(x, decimals) = rounded value to 'decimals' number of places
Example: Rounding Values
In the following example, we round an array of values to 2 decimal places using NumPy's round() function −
import numpy as np
# Define an array of values
values = np.array([3.14159, 2.71828, 1.61803, 0.57721])
# Round each element to 2 decimal places
rounded_values = np.round(values, 2)
print("Rounded values:", rounded_values)
The result of the above code is −
Rounded values: [3.14 2.72 1.62 0.58]
Flooring Using floor() Function
The numpy.floor() function rounds each element in the input array down to the nearest integer less than or equal to the element. This function always rounds down, regardless of the decimal part.
floor(x) = largest integer less than or equal to x
Example: Flooring Values
In the following example, we round down an array of values using NumPy's floor() function −
import numpy as np
# Define an array of values
values = np.array([3.14159, 2.71828, 1.61803, 0.57721])
# Round down each element to the nearest integer
floored_values = np.floor(values)
print("Floored values:", floored_values)
Following is the output obtained −
Floored values: [3. 2. 1. 0.]
Ceiling Using ceil() Function
The numpy.ceil() function rounds each element in the input array up to the nearest integer greater than or equal to the element. This function always rounds up, regardless of the decimal part.
ceil(x) = smallest integer greater than or equal to x
Example: Ceiling Values
In the following example, we round up an array of values using NumPy's ceil() function −
import numpy as np
# Define an array of values
values = np.array([3.14159, 2.71828, 1.61803, 0.57721])
# Round up each element to the nearest integer
ceiled_values = np.ceil(values)
print("Ceiled values:", ceiled_values)
This will produce the following result −
Ceiled values: [4. 3. 2. 1.]
Truncating Using trunc() Function
The numpy.trunc() function truncates each element in the input array by removing the decimal part. It essentially rounds the value towards zero, keeping the integer part.
trunc(x) = integer part of x, removing the decimal part
Example: Truncating Values
In the following example, we truncate an array of values using NumPy's trunc() function −
import numpy as np
# Define an array of values
values = np.array([3.14159, 2.71828, 1.61803, 0.57721])
# Truncate each element
truncated_values = np.trunc(values)
print("Truncated values:", truncated_values)
Following is the output of the above code −
Truncated values: [3. 2. 1. 0.]
Rounding to a Specific Multiple
The numpy.around() function is used to round elements in an array to the nearest specified multiple. This is different from the standard rounding because it allows for a custom rounding base.
around(x, decimals, out) = rounds to the nearest multiple of decimals
Example: Rounding to a Multiple
In the following example, we round an array of values to the nearest multiple of 0.5 using NumPy's around() function −
import numpy as np
# Define an array of values
values = np.array([3.14159, 2.71828, 1.61803, 0.57721])
# Round each element to the nearest multiple of 0.5
rounded_multiple_values = np.around(values * 2) / 2
print("Rounded to nearest multiple of 0.5:", rounded_multiple_values)
The output obtained is as shown below −
Rounded to nearest multiple of 0.5: [3. 2.5 1.5 0.5]