简体   繁体   English

浮点加法向上舍入

[英]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) . 因此,当将加数带到同一个指数时,我需要一个向上舍入的加法(即如果设置了一个数字移出一个数字,则加法应该用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)? 是否有一种简单的方法来执行此添加(手动非规范化较小的summand并使用nextval on spring,但我怀疑这会有效)?

You can set the FPU rounding mode to "upward" and then just add normally. 您可以将FPU舍入模式设置为“向上”,然后只需正常添加。

This is how it's done in GNU environments: 这是在GNU环境中完成的:

#include <fenv.h>

fesetround(FE_UPWARD);

If you have a Microsoft compiler, the equivalent code is: 如果您有Microsoft编译器,则等效代码为:

#include <float.h>

_set_controlfp(_RC_UP, _MCW_RC);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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