NumPy trunc()

The numpy.trunc() function truncates each element in an input array by removing its fractional part. The result is the nearest integer that is closer to zero than the input value.

Syntax

</>
Copy
numpy.trunc(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeInput array whose elements will be truncated.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the truncation.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array or scalar with the truncated values of the input elements. The fractional part of each number is removed, bringing the value closer to zero.


Examples

1. Truncating a Single Value

Here, we truncate a single floating-point value.

</>
Copy
import numpy as np

# Define a floating-point number
num = -3.75

# Compute the truncated value
result = np.trunc(num)

# Print the result
print("Truncated value:", result)

Output:

Truncated value: -3.0

2. Truncating an Array of Values

We apply truncation to multiple floating-point values in an array.

</>
Copy
import numpy as np

# Define an array of floating-point numbers
numbers = np.array([-2.8, -0.9, 3.4, 5.9, 7.1])

# Compute the truncated values
truncated_values = np.trunc(numbers)

# Print the results
print("Original numbers:", numbers)
print("Truncated values:", truncated_values)

Output:

Original numbers: [-2.8 -0.9  3.4  5.9  7.1]
Truncated values: [-2. -0.  3.  5.  7.]

3. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

# Define an array of floating-point numbers
numbers = np.array([1.5, -3.2, 4.8, -6.7])

# Create an output array with the same shape
output_array = np.empty_like(numbers)

# Compute truncated values and store them in output_array
np.trunc(numbers, out=output_array)

# Print the results
print("Computed truncated values:", output_array)

Output:

Computed truncated values: [ 1. -3.  4. -6.]

4. Using the where Parameter

Using a condition to apply truncation only to selected elements.

</>
Copy
import numpy as np

# Define an array of floating-point numbers
numbers = np.array([1.7, -2.4, 3.9, -4.5])

# Define a mask (apply truncation only where mask is True)
mask = np.array([True, False, True, False])

# Compute truncated values where mask is True
result = np.trunc(numbers, where=mask)

# Print the results
print("Computed truncated values with mask:", result)

Output:

Computed truncated values with mask: [1. 0. 3. 0.]

The truncation is applied only to elements where mask=True.