get and set floating point rounding mode
이전 댓글 표시
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개)
Divyanshu
2023년 11월 17일
0 개 추천
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.
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!