0
votes

I'm trying to find a good use for changing rounding mode in IEEE-754 floating-point environment.

I'm looking mostly from perspective of C and C++ code. There, it can be done using

int fesetround(int round)

function from "fenv.x" (or <cfenv> in C++).

The best use I could find is setting the environment to FE_TONEAREST, if for any godforsaken reason it wasn't the default.

In this question someone suggested using it to get some implementation-dependent behavior of string formatting. I personally find this answer completely wrong, and I believe changing rounding modes around formatting functions can only result in unexpected, or simply wrong behavior.

Another thing that is changed by rounding mode, are the functions: nearbyint and rint. But why would you ever want to use them, instead of dispatching to floor, ceil and trunc functions?

That leaves us with the only useful part being local difference in floating point arithmetic. Where could that be useful? I was trying to find some niche use for that, but so far I couldn't find any.

1
Have you looked at the niche "interval arithmetic"? Obviously, instructions with baked in rounding modes (like on GPUs, accessible via intrinsics) are more useful for that, but in a pinch a dynamically changeable rounding mode will do, at a cost in performance.njuffa

1 Answers

0
votes

The rouding modes have little to do with the floor and ceiling (the latter round to integers). I don't know about a trunk function.

Changing the rounding rule might be useful in some sophisticated cases where theoretical study shows that a particular rule has advantages over other or gives some desirable guarantee.