Re: Does the order matter to add a sequence of floating numbers
- From: Duncan Muirhead <dmuir@xxxxxxxxx>
- Date: Tue, 30 Oct 2007 17:12:50 +0000
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 orderYou can definitely get different answers if you have a wide range
matter? what is the different between arr[0] +..+ arr[99], and
arr[99]+...+arr[0]?
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)
.
- References:
- Does the order matter to add a sequence of floating numbers
- From: JosephLee
- Does the order matter to add a sequence of floating numbers
- Prev by Date: Binary search trees
- Next by Date: Re: Does the order matter to add a sequence of floating numbers
- Previous by thread: Re: Does the order matter to add a sequence of floating numbers
- Next by thread: Re: Does the order matter to add a sequence of floating numbers
- Index(es):
Relevant Pages
|