Version:

Last Modified: September 26, 2017

Rounding in the fixed-point data type occurs when the precision of the input value or the result of an operation is greater than the precision of the output type of an operator.

When rounding occurs, the value is coerced to a value with the same precision as the output type according to the rounding mode you select for the operator.

Many decimal (base 10) numbers are not exactly representable as binary (base 2) numbers. Likewise, the results of many arithmetic operations can only be represented exactly with long fractional parts or cannot be represented exactly at all. Because you necessarily define the fractional length of your fixed-point number or operator, you must designate a rounding mode when performing arithmetic with fixed-point data. The following table describes behavior of each rounding mode.

Rounding Mode | Behavior |
---|---|

Round Half to Even |
Rounds the value to the nearest value that the output type can represent. If the value to round is exactly between two representable values, the even value is chosen so that the least significant bit (LSB) after rounding is zero. This rounding mode has the largest impact on performance but produces the most accurate output values by neutralizing the bias towards higher values that occurs when you perform multiple rounding operations using the Round Half Up mode. In the Round Half to Even mode, any binary value with a least significant bit of zero is even, regardless of its decimal representation. Round Half to Even is the default rounding mode. |

Round Half Up |
Rounds the value to the nearest value that the output type can represent. If the value to round is exactly between two representable values, this rounding mode rounds the value up to the higher of the two valid values, in the positive direction. This rounding mode adds half a least significant bit to the output value and then truncates the value. This rounding mode produces more accurate results but has a larger resource cost than Round Down (truncate). |

Round Down (truncate) |
Rounds the value down toward negative infinity to the nearest value that the output type can represent. The least significant bits of the value are discarded. This rounding mode has the best performance but produces the least accurate output values. |

The following image shows the To Fixed-Point node converting the floating-point value 2.5 to a U4 <4.0> fixed-point configuration.

The floating-point input in the previous image requires at least a U3 <2.1> fixed-point number to be represented exactly. This means that although the value 2.5 falls within the type max of the fixed-point operator in the previous image, the input value does not match the precision of the output type. The input is then rounded to match the precision of the U4 <4.0> output type according to the rounding mode you select. The following examples show the effect of each rounding mode on the example input.

Rounding Mode | FXP Configuration | Input Value (Decimal) | Coerced Value (Binary) | Coerced Value (Decimal) | Behavior |
---|---|---|---|---|---|

Round Half to Even |
U4 <4.0> | 2.5 | 0010 | 2 | The input value falls exactly between two representable values, so the value is rounded toward the even value with the LSB of 0. |

Round Half Up |
U4 <4.0> | 2.5 | 0011 | 3 | The input value falls half way between two representable values, so the value is rounded up to the higher of the two representable values. |

Round Down (truncate) |
U4 <4.0> | 2.5 | 0010 | 2 | The fractional length of the input value is truncated, and the input is coerced down to 2. |

The following image shows the To Fixed-Point node converting the floating-point value -2.5 to a I4 <4.0> fixed-point configuration.

The floating-point input in the previous image requires at least a I4 <3.1> fixed-point number to be represented exactly. This means that although the value -2.5 falls within the type max of the fixed-point operator in the previous image, the input value does not match the precision of the output type. The input is then rounded to match the precision of the I4 <4.0> output type according to the rounding mode you select. The following examples show the effect of each rounding mode on the example input.

Rounding Mode | FXP Configuration | Input Value (Decimal) | Coerced Value (Binary) | Coerced Value (Decimal) | Behavior |
---|---|---|---|---|---|

Round Half to Even |
I4 <4.0> | -2.5 | 1110 | -2 | The input value falls exactly between two representable values, so the value is rounded toward the even of the two values. |

Round Half Up |
I4 <4.0> | -2.5 | 1110 | -2 | The input value falls half way between two representable values, so the value is rounded toward the higher value, in the positive direction. |

Round Down (truncate) |
I4 <4.0> | -2.5 | 1101 | -3 | The fractional length of the input value is truncated, and the input is coerced down toward negative infinity, to the representable value -3. |

A *rounding bias* occurs when rounding errors accumulate in your code during a series of operations. The rounding mode you select can help you control rounding biases that result from multiple rounding operations.

The following image shows input-to-round, a 16-bit fixed-point constant, multiplied by another value slightly less than 1. The result of each iteration of the For Loop becomes the new input-to-round value.

In this code, each time the precision of the result of the multiplication operation exceeds the precision of the output type configuration for the Multiply node, the result is rounded according to the specified rounding mode. The following table shows the outcome of this code in each rounding mode after 10 iterations.

Rounding Mode for Operator | Rounding Mode Result after 10 Iterations | Percent Error (from 64-bit calculation) | Behavior |
---|---|---|---|

Round Half to Even |
3.74566650390625 | +0.0065 % | This rounding mode accumulates the least amount of error through 10 iterations. |

Round Half Up |
3.7457275390625 | +0.0081 % | This rounding mode accumulates more error than Round Half to Even but has a smaller resource cost on hardware. |

Round Down (truncate) |
3.7451171875 | -0.0082 % | This rounding mode accumulates the most error through 10 iterations. This rounding mode produces a smaller result than the other rounding modes and a smaller result than an error-free calculation. |