# How to Scale a Fixed-Point Number to the Full Range of an Integer

Updated Jan 26, 2021

## Environment

### Software

• LabVIEW
• LabVIEW NXG
• LabVIEW FPGA Module
FPGA I/Os often use fixed-point numbers, but they can be difficult to understand and are not user friendly. There are some functions in LabVIEW FPGA that only accept integers and so we might want to convert a fixed-point number to an integer and have the integer use the full scale of the fixed-point number. For example, I have:
• A fix-point number (FXP) had a range of +/- 2.
• A 16-bit integer (I16).
• I want to convert -2 FXP to -32768 I16 and +2 FXP to +32767 I16.
• Any values between +/-2 FXP will be mapped to a corresponding value for I16, i.e. 0 FXP = 0 I16, and +1 FXP = +16384 I16.
This guide will give examples of how to convert from fixed-point numbers to integers or floating-point numbers, and visa versa, in LabVIEW. Similar functions can be used in LabVIEW NXG.
The functions in LabVIEW that can be used to convert fixed-point (FXP) to integers or floating points are:

### Scale Any Size FXP to an Integer

The Fixed-Point to Integer Cast can be used to convert FXP number to an integer and the size of the integer will be based off the size of FXP number. For example, if you have a FXP with a word length of 8 bits, this will be cast to a I8, but a FXP with word length of 9 will be cast to I16. (Word and integer word length descriptions )

This can be fine for some circumstances, however if we need convert any FXP (with any word length) to a particular size integer, for example any size FXP to I16, we need to cast that FXP to have a word length of 16 bits before using the Fixed-Point to Integer Cast:

1. Create your initial FXP number, this could be from a FPGA I/O, a constant or control.
1. If using a control or constant, you can right click on it, select Properties => Data Type and edit the word length and integer word length. Set word length = 4 bits and integer word length = 2 bits.
2. 2. Wire the FXP control (or constant) to a To Fixed Point conversion function, and set the output configuration to 16 bits.
1. Right click on To Fixed Point and select Properties => Output Configuration. Set Encoding to SignedWord length = 16 bits and Integer word length = 2 bits. This will ensure that the output is set to a 16 bit signed integer (I16), meaning the full range of the I16 number -32768 I16 to +32767 I16, whereas an unsigned 16 bit integer (U16) would have the range 0 to 65535
2. 3. Wire the output of To Fixed Point conversion function to a Fixed-Point to Integer Cast function. The output of this will be a 16-bit integer, scaled to the correct range.

### Convert Integer back to FXP

To convert back to a fixed point number, reverse the functions.
1. Wire the integer to a Integer to Fixed-Point function,
1. Wire a known FXP value into the fixed-point type input on the Integer to Fixed-Point Cast function. A control or constant can be used, as long as has the same properties (word and integer word length) as the initial FXP number to ensure its converted back in the correct format. Alternatively, wire the initial FXP value directly to the fixed-point type input.
1. Note: Its not a Required input, but its recommended to connect so you have more control over the functionality.
2. 2. The output of the Integer to Fixed-Point Cast will be a FXP number with the same properties as the initial FXP number. Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.

## Next Steps

To alter the integer range that the FXP number is converted to (i.e. I8 or I32), change the Output Configuration Word length for the initial To Fixed-Point conversion function.  i.e. set to 8 bits for I8 (-128 to +127) or 32 bits for (-2147486348 to +2147486347)