Rounding Functions
numpy.round
a: array_like - Input data.decimals: int, optional - Number of decimal places to round to (default: 0). May be negative.out: ndarray, optional - Alternative output array.
rounded_array: ndarray - An array of the same type asa.
- When
decimalsis 0, rounds to the nearest integer. - For values exactly halfway between rounded decimal values, rounds to the nearest even value.
- Behavior may vary for complex values.
numpy.floor
x: array_like - Input data.out: ndarray, optional - A location into which the result is stored.where: array_like, optional - Condition to broadcast over the input.
y: ndarray or scalar - The floor of each element inx.
x is the largest integer i such that i <= x.
Examples:
numpy.ceil
x: array_like - Input data.out: ndarray, optional - A location into which the result is stored.where: array_like, optional - Condition to broadcast over the input.
y: ndarray or scalar - The ceiling of each element inx.
x is the smallest integer i such that i >= x.
Examples:
numpy.trunc
x: array_like - Input data.out: ndarray, optional - A location into which the result is stored.where: array_like, optional - Condition to broadcast over the input.
y: ndarray or scalar - The truncated value of each element inx.
x is the nearest integer closer to zero. The fractional part of the signed number is discarded.
\lfloor x \rfloor & \text{if } x \geq 0 \\
\lceil x \rceil & \text{if } x < 0
\end{cases}$$
**Examples:**
```python
import numpy as np
a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
np.trunc(a)
# array([-1., -1., -0., 0., 1., 1., 2.])
# Truncation moves toward zero
np.trunc(-1.7), np.trunc(1.7)
# (-1.0, 1.0)
```
---
### numpy.rint
```python
numpy.rint(x, /, out=None, *, where=True, **kwargs)
```
Round elements of the array to the nearest integer.
**Parameters:**
- `x` : array_like - Input array.
- `out` : ndarray, optional - A location into which the result is stored.
- `where` : array_like, optional - Condition to broadcast over the input.
**Returns:**
- `out` : ndarray or scalar - Array of same shape and type as `x`.
**Notes:**
- Rounds to the nearest integer.
- When two integers are equally close, rounds to the nearest even integer.
- The result has the same data type as the input.
**Examples:**
```python
import numpy as np
a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
np.rint(a)
# array([-2., -2., -0., 0., 2., 2., 2.])
# Round-to-even for halfway cases
np.rint([0.5, 1.5, 2.5, 3.5])
# array([0., 2., 2., 4.])
```
---
### numpy.fix
```python
numpy.fix(x, out=None)
```
Round to nearest integer towards zero.
**Parameters:**
- `x` : array_like - An array of floats to be rounded.
- `out` : ndarray, optional - A location into which the result is stored.
**Returns:**
- `out` : ndarray of floats - The array of rounded numbers.
**Notes:**
`fix` is equivalent to `trunc` - it rounds toward zero, discarding the fractional part.
**Examples:**
```python
import numpy as np
np.fix([2.1, 2.9, -2.1, -2.9])
# array([ 2., 2., -2., -2.])
```
---
## Comparison of Rounding Functions
Different rounding functions behave differently, especially for negative numbers:
| Value | floor | ceil | trunc | rint | round |
|-------|-------|------|-------|------|-------|
| -1.7 | -2.0 | -1.0 | -1.0 | -2.0 | -2.0 |
| -1.5 | -2.0 | -1.0 | -1.0 | -2.0 | -2.0 |
| -0.2 | -1.0 | -0.0 | -0.0 | -0.0 | -0.0 |
| 0.2 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 |
| 1.5 | 1.0 | 2.0 | 1.0 | 2.0 | 2.0 |
| 1.7 | 1.0 | 2.0 | 1.0 | 2.0 | 2.0 |
| 2.5 | 2.0 | 3.0 | 2.0 | 2.0 | 2.0 |
| 3.5 | 3.0 | 4.0 | 3.0 | 4.0 | 4.0 |
**Visual Comparison:**
```python
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 100)
plt.figure(figsize=(12, 6))
plt.plot(x, x, 'k--', label='identity', alpha=0.3)
plt.plot(x, np.floor(x), label='floor')
plt.plot(x, np.ceil(x), label='ceil')
plt.plot(x, np.trunc(x), label='trunc')
plt.plot(x, np.rint(x), label='rint', linestyle=':')
plt.legend()
plt.grid(True, alpha=0.3)
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Comparison of Rounding Functions')
plt.show()
```
---
## Key Differences
<CardGroup cols={2}>
<Card title="floor()" icon="arrow-down">
Always rounds **down** to the next lower integer. For negative numbers, this means away from zero.
Example: `floor(-1.5) = -2`
</Card>
<Card title="ceil()" icon="arrow-up">
Always rounds **up** to the next higher integer. For negative numbers, this means toward zero.
Example: `ceil(-1.5) = -1`
</Card>
<Card title="trunc()" icon="scissors">
Rounds **toward zero** by removing the fractional part. Equivalent to `floor()` for positive, `ceil()` for negative.
Example: `trunc(-1.5) = -1`
</Card>
<Card title="rint() / round()" icon="circle-half-stroke">
Rounds to the **nearest integer**. For halfway cases (e.g., 1.5), rounds to the nearest **even** integer.
Example: `rint([0.5, 1.5]) = [0, 2]`
</Card>
</CardGroup>
---
## See Also
<CardGroup cols={2}>
<Card title="Arithmetic Functions" icon="calculator" href="/api/math/arithmetic">
Basic arithmetic operations
</Card>
<Card title="Trigonometric Functions" icon="wave-sine" href="/api/math/trigonometric">
sin, cos, tan, and their inverses
</Card>
<Card title="Exponential Functions" icon="chart-line" href="/api/math/exponential">
exp, log, and power functions
</Card>
<Card title="Special Functions" icon="square-root" href="/api/math/special">
sqrt, square, gcd, and lcm
</Card>
</CardGroup>