Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Round toward nearest integer with ties rounding toward positive infinity

`y = nearest(a)`

`y = nearest(a)`

rounds `fi`

object `a`

to
the nearest integer or, in case of a tie, to the nearest integer in
the direction of positive infinity, and returns the result in `fi`

object `y`

.

`y`

and `a`

have the same `fimath`

object
and `DataType`

property.

When the `DataType`

property of `a`

is `Single`

, `Double`

,
or `Boolean`

, the `numerictype`

of `y`

is
the same as that of `a`

.

When the fraction length of `a`

is zero or
negative, `a`

is already an integer, and the `numerictype`

of `y`

is
the same as that of `a`

.

When the fraction length of `a`

is positive,
the fraction length of `y`

is `0`

,
its sign is the same as that of `a`

, and its word
length is the difference between the word length and the fraction
length of `a`

, plus one bit. If `a`

is
signed, then the minimum word length of `y`

is `2`

.
If `a`

is unsigned, then the minimum word length
of `y`

is `1`

.

For complex `fi`

objects, the imaginary and
real parts are rounded independently.

`nearest`

does not support `fi`

objects
with nontrivial slope and bias scaling. Slope and bias scaling is
trivial when the slope is an integer power of 2 and the bias is 0.

The following example demonstrates how the `nearest`

function
affects the `numerictype`

properties of a signed `fi`

object
with a word length of 8 and a fraction length of 3.

a = fi(pi, 1, 8, 3) a = 3.1250 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 3 y = nearest(a) y = 3 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 6 FractionLength: 0

The following example demonstrates how the `nearest`

function
affects the `numerictype`

properties of a signed `fi`

object
with a word length of 8 and a fraction length of 12.

a = fi(0.025,1,8,12) a = 0.0249 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 12 y = nearest(a) y = 0 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 2 FractionLength: 0

The functions `convergent`

, `nearest`

and `round`

differ
in the way they treat values whose least significant digit is 5:

The

`convergent`

function rounds ties to the nearest even integerThe

`nearest`

function rounds ties to the nearest integer toward positive infinityThe

`round`

function rounds ties to the nearest integer with greater absolute value

The following table illustrates these differences for a given `fi`

object `a`

.

a | convergent(a) | nearest(a) | round(a) |
---|---|---|---|

–3.5 | –4 | –3 | –4 |

–2.5 | –2 | –2 | –3 |

–1.5 | –2 | –1 | –2 |

–0.5 | 0 | 0 | –1 |

0.5 | 0 | 1 | 1 |

1.5 | 2 | 2 | 2 |

2.5 | 2 | 3 | 3 |

3.5 | 4 | 4 | 4 |

`ceil`

| `convergent`

| `fix`

| `floor`

| `round`

Was this topic helpful?