## Numeric Considerations with Native Floating-Point

Native floating-point technology can generate HDL code from your floating-point design. Floating-point designs have better precision, higher dynamic range, and a shorter development cycle than fixed-point designs. If your design has complex math and trigonometric operations, use native floating-point technology.

HDL Coder™ generates code that complies with the IEEE-754 standard of floating-point arithmetic. HDL Coder native floating-point supports:

• Round to nearest rounding mode

• Denormal numbers

• Exceptions such as NaN (Not a Number), Inf, and Zero

• Customization of ULP (Units in the Last Place) and relative accuracy

### Round to Nearest Rounding Mode

HDL Coder native floating-point uses the round to nearest even rounding mode. This mode resolves all ties by rounding to the nearest even digit.

This rounding method requires at least three trailing bits after the 23 bits of the mantissa. The MSB is called Guard bit, the middle bit is called the Round bit, and the LSB is called the Sticky bit. The table shows the rounding action that HDL Coder performs based on different values of the three trailing bits. `x` denotes a don’t care value and can take either a 0 or a 1.

Rounding bitsRounding Action
`0xx`

No action performed.

`100`A tie. If the mantissa bit that precedes the Guard bit is a 1, round up, otherwise no action is performed.
`101`Round up.
`11x`Round up.

### Denormal Numbers

Denormal numbers are numbers that have an exponent field equal to zero and a nonzero mantissa field. The leading bit of the mantissa is zero.

`$value={\left(-1\right)}^{sign}*\left(0+\underset{i=1}{\stackrel{23}{\Sigma }}{b}_{23-i}{2}^{-i}\right)*{2}^{-126}$`

Denormal numbers have magnitudes less than the smallest floating-point number that can be represented without leading zeros in the mantissa. The presence of denormal numbers indicates loss of significant digits that can accumulate over subsequent operations and eventually result in unexpected values.

The logic to handle denormal numbers involves counting the number of leading zeros and performing a left shift operation to obtain the normalized representation. Addition of this logic increases the area footprint on the target device and can affect the timing of your design.

When using native floating-point support, you can specify whether you want HDL Coder to handle denormal numbers in your design.

### Exception Handling

If you perform operations such as division by zero or compute the logarithm of a negative number, HDL Coder detects and reports exceptions. The table summarizes the mapping from the encoding of a floating-point number to the value of the number for various kinds of exceptions. `x` denotes a don’t care value and can take either a 0 or a 1.

SignExponentSignificandValueDescription
x0xFF0x00000000

`$value={\left(-1\right)}^{S}\infty$`

Infinity
x0xFFA nonzero value

value = NaN

Not a Number
x0x000x00000000

value = 0

Zero
x0x00A nonzero value

`$value={\left(-1\right)}^{sign}*\left(0+\underset{i=1}{\stackrel{23}{\Sigma }}{b}_{23-i}{2}^{-i}\right)*{2}^{-126}$`

Denormal
x0x00 < E < 0xFFx

`$value={\left(-1\right)}^{sign}*\left(1+\underset{i=1}{\stackrel{23}{\Sigma }}{b}_{23-i}{2}^{-i}\right)*{2}^{\left(e-127\right)}$`

Normal

### Relative Accuracy and ULP Considerations

The representation of infinitely real numbers with a finite number of bits requires an approximation. This approximation can result in rounding errors in floating-point computation. To measure the rounding errors, the floating-point standard uses relative error and ULP (Units in the Last Place) error.

#### ULP

If the exponent range is not upper-bounded, Units in Last Place (ULP) of a floating-point number x is the distance between two closest straddling floating-point numbers a and b nearest to x. The IEEE-754 standard requires that the result of an elementary arithmetic operation such as addition, multiplication, and division is correctly round. A correctly rounded result means that the rounded result is within 0.5 ULP of the exact result.

An ULP of one means adding a `1` to the decimal value of the number. The table shows the approximation of pi to nine decimal digits and how the ULP of one changes the approximate value.

Floating-point numberValue in decimalIEEE-754 representation for Single TypesULP
3.14159274110785300110|10000000|100100100001111110110110
3.14159297910785300120|10000000|100100100001111110111001

The gap between two consecutively representable floating-point numbers varies according to magnitude.

Floating-point numberValue in decimalIEEE-754 representation for Single TypesULP
123456712346133040|10010011|001011010110100001110000
1234567.12512346133050|10010011|001011010110100001110011

#### Relative Error

Relative error measures the difference between a floating-point number and the approximation of the real number. Relative error returns the distance from 1.0 to the next larger number. This table shows how the real value of a number changes with the relative accuracy.

Floating-point numberValue in decimal IEEE-754 representation for Single TypesULPRelative error
838860812582912000|10010110|0000000000000000000000001
838860712582911980|10010101|1111111111111111111111012.3841858e-07
110653532160|01111111|0000000000000000000000001.1920929e-07
210737418240|10000000|0000000000000000000000012.3841858e-07

The magnitude of the relative error depends on the real value of the floating-point number.

In MATLAB®, the `eps` function measures the relative accuracy of the floating-point number. For more information, see `eps`.