Accelerating the pace of engineering and science

• Trials

# num2fixpt

Convert number to nearest value representable by specified fixed-point data type

## Syntax

```outValue = num2fixpt(OrigValue, FixPtDataType, FixPtScaling,
RndMeth, DoSatur)

```

## Description

num2fixpt(OrigValue, FixPtDataType, FixPtScaling, RndMeth, DoSatur) returns the result of converting OrigValue to the nearest value representable by the fixed-point data type FixPtDataType. Both OrigValue and outValue are of data type double. As illustrated in the example that follows, you can use num2fixpt to investigate quantization error that might result from converting a number to a fixed-point data type. The arguments of num2fixpt include:

 OrigValue Value to be converted to a fixed-point representation. Must be specified using a double data type. FixPtDataType The fixed-point data type used to convert OrigValue. FixPtScaling Scaling of the output in either Slope or [Slope Bias] format. If FixPtDataType does not specify a generalized fixed-point data type using the sfix or ufix command, FixPtScaling is ignored. RndMeth Rounding technique used if the fixed-point data type lacks the precision to represent OrigValue. If FixPtDataType specifies a floating-point data type using the float command, RndMeth is ignored. Valid values are Zero, Nearest, Ceiling, or Floor (the default). DoSatur Indicates whether the output should be saturated to the minimum or maximum representable value upon underflow or overflow. If FixPtDataType specifies a floating-point data type using the float command, DoSatur is ignored. Valid values are on or off (the default).

## Examples

Suppose you wish to investigate the quantization effect associated with representing the real-world value 9.875 as a signed, 8-bit fixed-point number. The command

```num2fixpt(9.875, sfix(8), 2^-1)

ans =

9.50000000000000```

reveals that a slope of 2^-1 results in a quantization error of 0.375. The command

```num2fixpt(9.875, sfix(8), 2^-2)

ans =

9.75000000000000```

demonstrates that a slope of 2^-2 reduces the quantization error to 0.125. But a slope of 2^-3, as used in the command

```num2fixpt(9.875, sfix(8), 2^-3)

ans =

9.87500000000000```

eliminates the quantization error entirely.