get and set floating point rounding mode
Show older comments
I have used two ways to get and set rounding mode. First using rnd = feature('setround'); feature('setround',rnd) with rnd = 0,Inf,-Inf or .5 .
The secon approch with fegetround() ; fesetround(rnd) with rnd = FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO. This C function is called via an appropriate mex file.
Both approach works fine, i.e. computations are done using the specified rounding mode. However there is a strange thing if I set a rounding mode using the C function and get the result with rnd = feature('setround'); rnd is not the attended mode although computation are executed with the set rounding mode.
For example, fesetround(FE_DOWNWARD) gives 1+1e-30 = 1 et 1-1e-30 = 9.999999999999998e-01 which is OK and rnd = feature('setround') ==> rnd = Inf.
Any idea?
Best regards
Alain
1 Comment
Alain Barraud
on 20 Nov 2023
Answers (1)
Divyanshu
on 17 Nov 2023
0 votes
Hi Alian,
I understand that you were expecting the output of 'feature('setround')' to be same as the rounding mode being set using the C function 'fesetround(FE_DOWNWARD)'. Please go through the following points for better understanding:
- Firstly, the C functions are independent of MATLAB functions and the changes made by one may not reflect on other side.
- Moreover, 'feature('setround')' is an undocumented function of MATLAB which may or may not give correct results, hence it is not recommended.
1 Comment
Alain Barraud
on 17 Nov 2023
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!