round

Rounds values to specified precision.

Syntax

R = round(x)

R = round(x,n)

R = round(x,n,'type')

Inputs

x
The values to round.
Type: double
Dimension: scalar | matrix
n
The number of places to round.
See 'type' for the options.
Type: integer
Dimension: scalar
'type'
The type of rounding to perform.
The available options are:
'decimal' (default)
Round to n digits (base 10) to the right of the decimal for positive n, and to the left for negative n (default: 0).
'significant'
Round to n significant digits (base 10). A positive integer is required.
'binary'
Round to n bits to the right of the decimal for positive n, and to the left for negative n (default: 0).
'sigbits'
Round to n significant bits. A positive integer is required.
Type: string

Outputs

R
The rounded values.
Type: double
Dimension: scalar | matrix

Examples

Scalar case:
R = round(0.5)
R = 1
Matrix case:
R = round([0.437, 16.605, 0.570, -8.749, -0.594, -0.621], 2, 'decimal')
R = [Matrix] 1 x 6
0.44000  16.61000  0.57000  -8.75000  -0.59000  -0.62000
Binary case: Round two nearly equal values to be equal, retaining maximum precision.
a1 = 2048 * (3 * 0.1)
a2 = 2048 * 0.3
delta1 = a1 - a2    % rounding difference
bits = -log2(abs(delta1)) - 1
r1 = round(a1, bits, 'binary')
r2 = round(a2, bits, 'binary')
delta2 = r1 - r2    % no rounding difference

a1 = 614.4
a2 = 614.4
delta1 = 1.13687e-13
bits = 42
r1 = 614.4
r2 = 614.4
delta2 = 0

Comments

When x is complex, the rounding is applied to the real and imaginary components.

It should be understood that results from rounding in base 10 are constrained by binary limitations. A value such as 0.1 (base 10) does not have a finite precision representation in base 2 because it has a repeating bit pattern.

The 'binary' and 'sigbits' options are useful when analyzing values whose differences are too small to be observed in base 10.

Double precision (64 bit) values in IEEE 754 have 53 explicit bits, with the leading bit being an implied 1. The 'sigbits' option treats the implied leading bit as significant.