qformatpy.rounding¶
- qformatpy.rounding(iarray, rnd_method: str = 'Trunc')[source]¶
Rounds each element in the input array according to the specified rounding method.
- Parameters:
- iarraynumpy.ndarray
Input array containing numerical values.
- rnd_methodstr
- Rounding method to be applied. Supported methods:
‘Trunc’: Round towards -inf.
‘Ceiling’: Round towards +inf.
‘TowardsZero’: Round towards zero.
‘AwayFromZero’: Round away from zero.
‘HalfUp’: Round half up.
‘HalfDown’: Round half down.
‘HalfTowardsZero’: Round half towards zero.
‘HalfAwayFromZero’: Round half away from zero.
Default is ‘Trunc’
- Returns:
- numpy.ndarray
Array of rounded values based on the specified rounding method.
- Raises:
- ValueError
If an invalid rounding method is provided.
Examples
>>> import numpy as np >>> from qformatpy import rounding >>> input_array = np.array([3.7, 2.2, -5.5]) >>> rounding(input_array, 'Trunc') array([ 3, 2, -6])
The rounding method can be changed to ‘HalfUp’:
>>> rounding(input_array, 'HalfUp') array([ 4, 2, -5])