Re: Does the order matter to add a sequence of floating numbers



On Tue, 30 Oct 2007 16:10:31 +0000, JosephLee wrote:

Let's say float arr[100], we want to add them up, does the order
matter? what is the different between arr[0] +..+ arr[99], and
arr[99]+...+arr[0]?
You can definitely get different answers if you have a wide range
of sizes. For example

int main( int argc, char** argv)
{
float xs[] = { 1, 1e9, -1e9, 2};
float sum, sumr;
int i;
int n = sizeof xs / sizeof xs[0];
for( sum=sumr=0.0, i=0; i<n; ++i)
{ sum += xs[i];
sumr += xs[n-1-i];
}
printf( "sum %15.7e, sumrev %15.7e, diff %15.7e\n", sum, sumr, sum-sumr);
return EXIT_SUCCESS;
}
prints
sum 2.0000000e+00, sumrev 1.0000000e+00, diff 1.0000000e+00

(because 1+1e9 == 1e9 as floats, etc)

.



Relevant Pages

  • Re: =sumif
    ... >> I need to sum the combined quantities of the same box sizes in B33 to B36 ... >> against the different box sizes in A33 to B36 ...
    (microsoft.public.mac.office.excel)
  • Re: [TCL] diff type operations in TCL
    ... unix "sum" for a quick diff. ... if your using a unix command cmp would be better ... Bruce ...
    (comp.lang.tcl)
  • Re: sizeof unpadded struct size
    ... sum the sizes of the members of the struct. ...
    (comp.lang.c)
  • Re: Sum every Third Row
    ... I now need to SUM every third row, Column C, ... Every ROW that has DIFF: ... User ID Route # Pallet Jacks Plastic Pallets Wood Pallets Blankets I/C ...
    (microsoft.public.excel.worksheet.functions)
  • Embedded Matlab Fcn and HDL Coder Data Types
    ... I want to generate HDL code. ... if (V_inj> sum) ... if (V_inj < diff) ... to introduce VHDL datatypes ...
    (comp.soft-sys.matlab)