# trunc#

ivy.trunc(x, /, *, out=None)[source]#

Round each element x_i of the input array x to the integer-valued number that is closest to but no greater than x_i.

Special cases

• If x_i is already an integer-valued, the result is x_i.

For floating-point operands,

• If x_i is +infinity, the result is +infinity.

• If x_i is -infinity, the result is -infinity.

• If x_i is +0, the result is +0.

• If x_i is -0, the result is -0.

• If x_i is NaN, the result is NaN.

Parameters:
• x (Union[Array, NativeArray]) – input array. Should have a numeric data type.

• out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – an array containing the rounded result for each element in x. The returned array must have the same data type as x.

This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances in place of any of the arguments

Examples

With ivy.Array input:

>>> x = ivy.array([-1, 0.54, 3.67, -0.025])
>>> y = ivy.trunc(x)
>>> print(y)
ivy.array([-1.,  0.,  3., -0.])
>>> x = ivy.array([0.56, 7, -23.4, -0.0375])
>>> ivy.trunc(x, out=x)
>>> print(x)
ivy.array([  0.,   7., -23.,  -0.])
>>> x = ivy.array([[0.4, -8, 0.55], [0, 0.032, 2]])
>>> y = ivy.zeros([2,3])
>>> ivy.trunc(x, out=y)
>>> print(y)
ivy.array([[ 0., -8.,  0.],
[ 0.,  0.,  2.]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([-0.25, 4, 1.3]), b=ivy.array([12, -3.5, 1.234]))
>>> y = ivy.trunc(x)
>>> print(y)
{
a: ivy.array([-0., 4., 1.]),
b: ivy.array([12., -3., 1.])
}
Array.trunc(self, *, out=None)[source]#

ivy.Array instance method variant of ivy.trunc. This method simply wraps the function, and so the docstring for ivy.trunc also applies to this method with minimal changes.

Parameters:
• self (Array) – input array. Should have a real-valued data type.

• out (Optional[Array], default: None) – optional output, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – an array containing the rounded result for each element in self. The returned array must have the same data type as self

Examples

>>> x = ivy.array([-1, 0.54, 3.67, -0.025])
>>> y = x.trunc()
>>> print(y)
ivy.array([-1.,  0.,  3., -0.])
Container.trunc(self, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.trunc. This method simply wraps the function, and so the docstring for ivy.trunc also applies to this method with minimal changes.

Parameters:
• self (Container) – input container. Should have a real-valued data type.

• key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

• to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

• prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

• map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

• out (Optional[Container], default: None) – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – a container containing the rounded result for each element in self. The returned container must have the same data type as self.

Examples

>>> x = ivy.Container(a=ivy.array([-0.25, 4, 1.3]),
...                   b=ivy.array([12, -3.5, 1.234]))
>>> y = x.trunc()
>>> print(y)
{
a: ivy.array([-0., 4., 1.]),
b: ivy.array([12., -3., 1.])
}