NumPy trunc() and fix(): How to Round Down an Array of Numbers

Updated: January 23, 2024 By: Guest Contributor Post a comment

Overview

NumPy is an essential library in the Python ecosystem, widely used for scientific computing due to its powerful array objects and a wide range of tools to process those arrays. Among its plethora of functions, NumPy provides two lesser-known ones that are handy when dealing with floating-point arrays: trunc() and fix(). These functions allow you to round down numbers in an array, and they do so in a manner that might not be immediately intuitive. In this tutorial, we will explore how these functions operate and in what scenarios to use them.

Introduction to NumPy trunc()

The trunc() function is a part of the mathematical functions in NumPy, and its primary role is to return the truncated version of the input, discarding the fractional part. Now, let’s dive into some examples:

Basic Example of trunc()

import numpy as np

# Create an array of floating-point numbers
arr = np.array([3.667, 4.123, 5.789, 6.999, -2.392, -3.444])

# Apply the trunc() function
t_truncated = np.trunc(arr)

# Output the result
print("Truncated array:", t_truncated)

Output:

Truncated array: [ 3.  4.  5.  6. -2. -3.]

As you can see, np.trunc() simply cuts off the decimal part of each number, resulting in the closest integer that is less than or equal to the number if it’s positive, and greater than or equal to the number if it’s negative.

Vectorized truncation

One of the strengths of NumPy is its ability to work with arrays in a vectorized manner. This means operations can be performed on entire arrays rather than needing to loop through elements. The following example shows truncation working across a two-dimensional array:

import numpy as np

# Create a 2D array of floating-point numbers
arr_2d = np.array([[3.667, 4.123], [5.789, 6.999]])

# Apply the trunc() function to 2D array
t_truncated_2d = np.trunc(arr_2d)

# Output the result
print("Truncated 2D array:\n", t_truncated_2d)

Output:

Truncated 2D array:
 [[3. 4.]
 [5. 6.]]

Again, np.trunc() operates on each element of the array, ignoring the fractional part, regardless of the array’s shape. Next, we will explore the np.fix() function.

Introduction to NumPy fix()

The fix() function behaves in a similar manner to trunc(). It also discards the fractional part of a floating-point number, but with a slight twist in how it handles negative numbers. Let’s see some examples.

Basic Example of fix()

import numpy as np

# Create an array of floating-point numbers
arr = np.array([3.567, 4.823, -2.678, -3.999])

# Apply the fix() function
f_fixed = np.fix(arr)

# Output the result
print("Fixed array:", f_fixed)

Output:

Fixed array: [ 3.  4. -2. -3.]

The np.fix() function behaves exactly like np.trunc() in that it removes the decimal and returns the nearest integer towards zero. Now, let’s examine fix() with a two-dimensional array.

Vectorized fixation

import numpy as np

# Create a 2D array of floating-point numbers
arr_2d = np.array([[3.567, 4.823], [-2.678, -3.999]])

# Apply the fix() function to a 2D array
f_fixed_2d = np.fix(arr_2d)

# Output the result
print("Fixed 2D array:\n", f_fixed_2d)

Output:

Fixed 2D array:
 [[ 3.  4.]
 [-2. -3.]]

Advanced Uses of trunc() and fix()

Both trunc() and fix() can be useful in more complex scenarios where you need to perform additional array operations during the rounding process.

Combined with Universal Functions

NumPy’s universal functions (ufuncs) can be coupled with trunc() and fix() for more complex expressions:

import numpy as np

# Create an exponential array
arr = np.exp(np.arange(4))

# Truncate after applying the exponential
exp_truncated = np.trunc(arr)

# Display the transformation
for a, e in zip(arr, exp_truncated):
    print(f"Original: {a}, Truncated: {e}")

Output:

Original: 1.0, Truncated: 1.0
Original: 2.718281828459045, Truncated: 2.0
Original: 7.38905609893065, Truncated: 7.0
Original: 20.085536923187668, Truncated: 20.0

Conclusion

In summary, both np.trunc() and np.fix() offer a straightforward method for rounding down numbers in a floating-point array by removing their decimal parts. The difference between the two functions is more philosophical than practical since they can be used interchangeably for most applications. However, becoming familiar with their nuances can aid in code readability and maintainability in cases where the rounding direction’s emphasis is valuable.