随机排序的IEEE 754双精度浮点数之和的结果 [英] Result of the sum of random-ordered IEEE 754 double precision floats

查看:120
本文介绍了随机排序的IEEE 754双精度浮点数之和的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题的伪代码.

Here is a pseudocode of my problem.

我有一个 IEEE 754 双精度正数数组.

I have an array of IEEE 754 double precision positive numbers.

数组可以按随机顺序排列,但数字始终相同,只是位置混乱.同样,这些数字可以在double表示形式的有效IEEE范围内很大范围内变化.

The array can come in a random order but numbers are always the same, just scrambled in their positions. Also these numbers can vary in a very wide range in the valid IEEE range of the double representation.

一旦有了列表,我就会初始化一个变量:

Once I have the list, I initialize a variable:

double sum_result = 0.0;

然后在整个数组的循环中,将和累加到sum_result上.在每一步中,我都会这样做:

And I accumulate the sum on sum_result, in a loop over the whole array. At each step I do:

sum_result += my_double_array[i]

是否保证无论double初始数组的顺序如何,如果数字相同,则打印出的总和结果将始终相同?

Is it guaranteed that, whatever the order of the initial array of double, if the numbers are the same, the printed out sum result will be always the same?

推荐答案

是否可以保证,无论初始double数组的顺序如何,如果数字相同,则打印出的总和结果将始终相同?

Is it guaranteed that, whatever the order of the initial array of double, if the numbers are the same, the printed out sum result will be always the same?

否,FP添加不是关联.请记住,它称为 floating 浮点-相对于1.0的绝对精度浮点数".任何给定的操作(如加法(+))都应受

No, FP addition is not associative. Remember it is called floating point - the absolute precision "floats" about relative to 1.0. Any given operation like addition (+) is subject to round-off error.

但是,如果完成了总和并且 inexact 标志被清除,那么是的,顺序是不相关的.**

Yet if the sum is done and the inexact flag is clear, then yes, the order was not relevant.**

简单的计数器示例.

#include <math.h>
#include <float.h>
#include <fenv.h>
#include <stdio.h>

int main(void) {
  double a[3] = { DBL_MAX, -DBL_MAX, 1.0 };
  fexcept_t flag;

  feclearexcept(FE_ALL_EXCEPT);
  printf("%e\n", (a[0] + a[1]) + a[2]);
  fegetexceptflag(&flag, FE_INEXACT);
  printf("Inexact %d\n", !!(flag & FE_INEXACT));

  feclearexcept(FE_ALL_EXCEPT);
  printf("%e\n", a[0] + (a[1] + a[2]));
  fegetexceptflag(&flag, FE_INEXACT);
  printf("Inexact %d\n", !!(flag & FE_INEXACT));

  printf("%d\n", FLT_EVAL_METHOD);
  return (EXIT_SUCCESS);
}

输出

1.000000e+00  // Sum is exact
Inexact 0

0.000000e+00  // Sum is inexact
Inexact 1

0    // evaluate all operations ... just to the range and precision of the type;

根据FLT_EVAL_METHOD,FP数学可能会使用更宽泛的进动范围,但上述极端示例的总和仍会有所不同.

Depending on FLT_EVAL_METHOD, FP math may use wider precession and range, yet the above extreme example sums will still differ.

**可能是0.0 vs -0.0的结果

** aside from maybe a result of 0.0 vs -0.0

要了解原因,请尝试一个基于10的4位数精度的文本示例.相同的原理适用于double,它通常具有53个二进制数字精度.

To see why, try a based 10 text example with 4 digits of precision. The same principle applies to double with its usual 53 binary digits of precision.

a[3] = +1.000e99, -1.000e99, 1.000
sum = a[0] + a[1]   // sum now exactly 0.0 
sum += a[2]         // sum now exactly 1.0 
// vs.
sum = a[1] + a[2]   // sum now inexactly -1.000e99
sum += a[0]         // sum now inexactly 0.0


Re:打印出的总和结果将始终相同":除非代码使用"%a""%.*e"进行足够精确的打印,否则打印的文本可能没有意义,并且有两个不同的总和可能外观相同.请参见 Printf宽度说明符,以保持浮点值的精度


Re: "printed out sum result will be always the same" : Unless code prints with "%a" or "%.*e" with higher enough precision, the text printed may lack significance and two different sums may look the same. See Printf width specifier to maintain precision of floating-point value

这篇关于随机排序的IEEE 754双精度浮点数之和的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆