简体   繁体   English

c ++:浮点运算稳定性策略

[英]c++: strategies for stability of floating point arithmetic

Can anyone recommend any C++ libraries/routines/packages that contain strategies for maintaining the stability of various floating point operations? 任何人都可以推荐任何包含维护各种浮点运算稳定性的策略的C ++库/例程/包吗?

Example: suppose you would like to sum across a vector/array of one million long double in the unit interval (0,1), and that each number is of about the same order of magnitude. 示例:假设您希望在单位间隔(0,1)中对一百万个long double精度的矢量/数组求和,并且每个数字的大小大致相同。 Naively summing for (int i=0;i<1000000;++i) sum += array[i]; 天然求和for (int i=0;i<1000000;++i) sum += array[i]; is unreliable - for large enough i , sum will be of a much larger order of magnitude than array[i] , and so sum += array[i] would be equivalent to sum += 0.00 . 是不可靠的 - 对于足够大的isum将比array[i]具有更大的数量级,因此sum += array[i]将等于sum += 0.00 (Note: the solution to this example is a binary summing strategy.) (注意:此示例的解决方案是二进制求和策略。)

I deal with sums and products of thousands/millions of miniscule probabilities. 我处理数千/数百万微小概率的总和和产品。 I am using the arbitrary-precision library MPFRC++ with a 2048 bit significand, but the same concerns still apply. 我使用具有2048位有效数的任意精度库MPFRC++ ,但同样的问题仍然适用。

I am chiefly concerned with: 我主要关心的是:

  1. Strategies for accurately summing many numbers (eg above Example). 准确汇总许多数字的策略(例如上面的例子)。
  2. When is multiplication and division potentially unstable? 乘法和除法何时可能不稳定? (If I want to normalize a large array of numbers, what should my normalization constant be? The smallest value? The largest? A median?) (如果我想规范化大量数字,我的归一化常数应该是多少?最小值?最大?中位数?)

Binary summation doesn't guarantee accurate result. 二元求和并不能保证准确的结果。 The most reliable (albeit slower) method is to use Kahan summation . 最可靠(尽管速度较慢)的方法是使用Kahan求和 Boost.Accumulators has an implementation of the above and much more. Boost.Accumulators有上面的实现和更多。

Multiplication and division stability: unless you get to denormalized floats they don't suffer from the same problems as summation and substraction. 乘法和除法稳定性:除非你得到非规范化的浮点数,否则它们不会遇到与求和和减法相同的问题。 In fact the error of multiplication is at most 0.5 ulp (units last place). 事实上,乘法误差最多为0.5 ulp(单位最后一位)。

... what should my normalization constant be? ......我的归一化常数应该是多少?

What do you mean by 'normalize'? 'normalize'是什么意思? It depends on the norm you use. 这取决于您使用的规范 Possible candidates: use the maximum absolute value in the array, or any other generalized mean. 可能的候选者:使用数组中的最大绝对值,或任何其他广义均值。 (Other choices you listed do not work since they may be zero even for non-zero array.) (您列出的其他选项不起作用,因为即使非零数组也可能为零。)

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

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