简体   繁体   中英

floating point addition rounding upwards

I have a floating point addition that is somewhat likely to go wrong as the values have different magnitude, so quite a few significant digits are shifted out (possibly even all of them). In the scope of the entire calculation precision is not that relevant, only that the result is greater or equal to what would be the result with arbitrary precision (I'm keeping track of the end of a range here, and extend it by at least a certain amount).

So I'd need an addition that rounds up when bringing the summands to the same exponent (ie if one digit shifted out of a summand was set, the addition should take place with nextval(denormalized_summand, +infinity) .

Is there an easy way to perform this addition (manually denormalizing the smaller summand and using nextval on it springs to mind, but I doubt that would be efficient)?

You can set the FPU rounding mode to "upward" and then just add normally.

This is how it's done in GNU environments:

#include <fenv.h>

fesetround(FE_UPWARD);

If you have a Microsoft compiler, the equivalent code is:

#include <float.h>

_set_controlfp(_RC_UP, _MCW_RC);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM