필터 지우기
필터 지우기

get and set floating point rounding mode

조회 수: 8 (최근 30일)
Alain Barraud
Alain Barraud 2023년 11월 7일
댓글: Alain Barraud 2023년 11월 20일
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