get and set floating point rounding mode

12 次查看(过去 30 天)
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 个评论
Alain Barraud
Alain Barraud 2023-11-20
I answer to my own question. System_dependant or feature has an internal memory of the last setting. So when we get the current round mode with these function they don't get from the fpu its current rounding mode; the given answer is the last setting done. So if a new setting is done by my C code the matlab function cannot gives the good answer!
Both approches work fine there is no contradiction!

请先登录,再进行评论。

回答(1 个)

Divyanshu
Divyanshu 2023-11-17
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 个评论
Alain Barraud
Alain Barraud 2023-11-17
hello,
'feature('setround')' is used by many people (or system_dependent) and within well known package (interval arithmetic for example) for many years. Using matlab or the C function, both set something correctly within fpu, because checking how computations are done shows that in any case rounding mode is as desired. My question was : is it possible that matlab and C set two different PFU parameters having the same effect, consequently getting the rounding mode could give 2 differents results in some cases according to matlab or the C function call.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by