# nearestDiv

## Description

## Examples

### Divide and Round to Nearest

Perform a division operation and round to the nearest integer value.

nearestDiv(int16(201),10)

ans = 20 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 13 FractionLength: 0

Perform a division operation and round to the nearest multiple of 7.

nearestDiv(int16(201),10,7)

ans = 21 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 13 FractionLength: 0

### Divide and Generate Code

Define a function that uses nearest`Div`

.

function y = nearestDiv_example(x,d) y = nearestDiv(x,d); end

Define inputs and execute the function in MATLAB®.

x = fi(pi); d = fi(2); y = nearestDiv_example(x,d)

y = 1 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 2 FractionLength: 0

To generate code for this function, the denominator `d`

must be defined as a constant.

codegen nearestDiv_example -args {x, coder.Constant(d)}

Code generation successful.

Alternatively, you can define the denominator, `d`

, as constant in the body of the code.

function y = nearestDiv10(x) y = nearestDiv(x,10); end

x = fi(5*pi); y = nearestDiv10(x)

y = 1 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 2 FractionLength: 0

codegen nearestDiv10 -args {x}

Code generation successful.

## Input Arguments

`x`

— Dividend

scalar

Dividend, specified as a scalar.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `logical`

| `fi`

`d`

— Divisor

scalar

Divisor, specified as a scalar.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `logical`

| `fi`

`m`

— Value to round to nearest multiple of

`1`

(default) | scalar

Value to round to nearest multiple of, specified as a scalar.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `logical`

| `fi`

## Output Arguments

`y`

— Result of division and round to floor

scalar

Result of division and round to floor, returned as a scalar.

The datatype of `y`

is calculated such that the wordlength and
fraction length are of a sufficient size to contain both the largest and smallest
possible solutions given the data type of `x`

, and the values of
`d`

and `m`

.

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Slope-bias representation is not supported for fixed-point data types.

To generate code, the denominator `d`

must be declared as
constant.

### Fixed-Point Conversion

Design and simulate fixed-point systems using Fixed-Point Designer™.

Slope-bias representation is not supported for fixed-point data types.

## Version History

**Introduced in R2021a**

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)