Reputation: 11
For the purposes of my application the result of a fixed point calculation needs to be rounded to the 2nd decimal place first and then rounded to integer. So, if you consider a number like 0.4962
after you round it to 2 decimal places you get 0.5
. And once you round that to integer you get 1
.
Problem is some values are not getting rounded correctly.
Since MATLAB's round function does not support Nth decimal place rounding for arguments of type fi
I'm using the following algo:
Round
rounding methodLet's consider this code:
format long g
a = fi(33.495, 0, 64, 40)
b = fi(0, 0, 64, 40)
math = fimath('RoundingMethod', 'Round', 'ProductMode', 'SpecifyPrecision', 'ProductWordLength', 64, 'ProductFractionLength', 40)
b(:) = round(math.mpy(a, fi(100, 0, 8, 0)))
b(:) = divide(numerictype(0, 64, 40), b, 100)
Unfortunately the output of this isn't 33.5
as expected, but 33.49
Here's the complete output:
33.4949999999999
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 64
FractionLength: 40
b =
0
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 64
FractionLength: 40
math =
RoundingMethod: Round
OverflowAction: Saturate
ProductMode: SpecifyPrecision
ProductWordLength: 64
ProductFractionLength: 40
SumMode: FullPrecision
b =
3349
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 64
FractionLength: 40
b =
33.4899999999998
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 64
FractionLength: 40
What do I do?
Upvotes: 0
Views: 376
Reputation: 356
Matlab supports rounding a number upto a specified number of decimal points.
var = 0.4962
var = round(var,2) # (gives you 0.50)
var = round(var) # (gives you 1.0)
Upvotes: -1