MATLAB: How do the OverflowMode and RoundMode properties interact during fixed point conversion in MATLAB (R2011b)

fixedoverflowmodepointroundmode

The below commands are executed in MATLAB to perform fixed point conversion:
a = fi(0.999,'OverflowMode','wrap','RoundMode','ceil','Signedness','Unsigned','WordLength',8,'FractionLength',8)
b = fi(0.999,'OverflowMode','wrap','RoundMode','floor','Signedness','Unsigned','WordLength',8,'FractionLength',8)
This gives the output:
a = 0, b = 0.9961
In what order are the OverflowMode and RoundMode properties processed?

Best Answer

In the first case, since the 'OverflowMode' is 'wrap', the output will be zero once the limit is exceeded (for WordLength = 8, the limit is 255). Now, consider the Fixed point representation for 0.999:
(0.999*(2^8)) = 255.744
For 'RoundMode' = 'ceil', the final value is 256, which wraps around to zero according to the OverflowMode property. For 'RoundMode' = 'floor', the final value is 255, which gives output = 0.9961 (because 255/256 = 0.9961).
During fixed point conversion, we first need to round off the number to the nearest ceil or floor, and only then check for overflow. This is because, for the overflow check, we need to compare two integers (the input and the maximum integer that can be stored for the given wordlength and fraction length). In order to obtain an integer value for the given floating point input, we first need to perform rounding.