Effacer les filtres
Effacer les filtres

get and set floating point rounding mode

8 vues (au cours des 30 derniers jours)
Alain Barraud
Alain Barraud le 7 Nov 2023
Commenté : Alain Barraud le 20 Nov 2023
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 commentaire
Alain Barraud
Alain Barraud le 20 Nov 2023
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!

Connectez-vous pour commenter.

Réponses (1)

Divyanshu
Divyanshu le 17 Nov 2023
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 commentaire
Alain Barraud
Alain Barraud le 17 Nov 2023
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by