numpy
  1. numpy-trunc

trunc() - ( NumPy Functions )

Heading h2

Syntax

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

Example

import numpy as np

x = np.array([1.2, 3.9, 2.5, -3.2, -4.5])
y = np.trunc(x)

print(y)

Output

[ 1.  3.  2. -3. -4.]

Explanation

The trunc() function in NumPy returns the truncated value of the input elementwise. It returns the nearest integer towards 0 for each element of the input array.

Use

The trunc() function in NumPy is useful in situations where you need to truncate the decimal part of the elements in an array. This function is also useful in math and scientific calculations that require you to compute values using integer arithmetic.

Important Points

  • The trunc() function in NumPy returns the truncated value of the input elementwise
  • It returns the nearest integer towards 0 for each element of the input array
  • The trunc() function is useful in situations where you need to truncate the decimal part of the elements in an array
  • The function is also useful in math and scientific calculations that require you to compute values using integer arithmetic

Summary

In conclusion, the trunc() function in NumPy is a useful function that returns the truncated value of the input elementwise. It is useful in situations where you need to truncate the decimal part of the elements in an array and in math and scientific calculations that require you to compute values using integer arithmetic. By using NumPy's trunc() function, you can perform these operations quickly and efficiently in your Python code.

Published on: