Problem with summation algorithm

In summary, the programmer is trying tosum up a list of floats and compare it to a certain algorithm. They are having trouble with two seg faults randomly occurring. One seg fault seems to happen when they compile and execute the code for the same value of N a few times, but sometimes it doesn't. The other seg fault seems to occur when N is greater than 18. Last, they are also having trouble with their sums sometimes giving incorrect values.
  • #1
Dathascome
55
0
Hi there, I'm trying to right a program for class that 1st assigns random single precission floats from 0 to 1 to the elements 1-d array and then sums them up. Next I'm supposed to compare to this thing called the Kahan summation algorithm for different values of N (array size) using the fractional difference.
I'm just trying to get each of the sums to print out their values now to make sure they doing what they should be doing, not worrying about the fractional difference part yet but keep getting seg faults and I have no idea why.
What's weird is that I think I'm getting them in two different instances. One seg fault seems to come randomly...like I compile and execute the code for the same value of N a few times and every now and then it seg faults, but sometimes not.
The other seg fault seems to occur if N>18.
Last thing...something weird is happening with my sums also because sometimes for som given value like N=15 it gives me the correct summation values, and then every now and again it gives me something ridiculous like sum = 4113476354484640333955072.00000.

Here's my code:
Code:
#include <stdio.h>
#include <stdlib.h> 
#include <sys/types.h>
#include <time.h>
#include <assert.h>

float brute(long N,float A[N])
{
        long i;
	float sum = 0;
	for(i=0; i<N; i++)
		sum+=A[i];
	return sum;
}

float kahan(long N,float A[N])
{
	float sum = A[0];
	float  c = 0;
	float y,t;         
	long i;
	for(i=1; i<N; i++)
	{
	  y = A[i] - c ;   
	  t = sum + y;  
	  c = (t - sum) - y;
	  sum = t;       
	}         

	return sum;
}

int main(int argc,char *argv[])
{
	
	long i, N, iSeed;
	float  A[N], bsum, ksum;
	if (argc != 2) 
	{
	        fprintf(stderr,"Usage: %s N, where N is the size of the array.\n",argv[0]);
			return 1;
	}
	N = atoi(argv[1]);
	assert(N>1);
	iSeed = (int) time(NULL) % getpid() + getppid();
	printf("Seed = %i\n",iSeed);
	srand48(iSeed);

	for(i=0;i<N;i++)
	{
		A[i]=drand48();
		printf("elems of A are %f\n",A[i]);
	}
        bsum = brute(N,A);
	ksum = kahan(N,A);
	printf("bsum is %f, ksum is %f\n",bsum,ksum);
return 0;

The print statements I just have in there to see what's going on for now.
Any insight at all would be greatly appreciated and would help me keep my sanity
 
Technology news on Phys.org
  • #2
Code:
int main(int argc, char *argv[])
{
   long i, N, iSeed;
At this point in the program, N is uninitialized: it could be anything from -2147483648 to -2147483647 (on a 32-bit machine).

Code:
   float A[N], bsum, ksum;
This initializes the array A to have length N. And since N is still uninitialized...



Incidentally, if you enable warnings for your C compiler, it would probably have told you this. The option is -Wall if you're using gcc.


Oh, one more thing: const correctness is a good habit, and there's no time like the present to start learning! :smile: Both of your functions do not modify the data; so, for example, you should make the declaration

Code:
float kahan(long N, const float A[N])

Some benefits are:
(1) It let's the compiler catch some of your mistakes
(2) It prevents headaches when you try to interface with other const-correct code
(3) It can result in a faster program
(4) It's "self-documenting": if someone is reading your program, it instantly tells them that this function will not modify A.
 
Last edited:
  • #3
Thanks much Hurkyl. I changed it so that I initialized N when I first declared it and it seems to work just fine now. Despite that I'm not sure why still. Is the problem just that I tried to use it in A[N] before I specified what N was? I guess that makes sense then.
Also I tried gcc -Wall with my old code and it didn't give me any warning about that, all it said was:
Code:
ps2_1.c: In function 'main':
ps2_1.c:45: warning: implicit declaration of function 'getpid'
ps2_1.c:45: warning: implicit declaration of function 'getppid'

As for your last comment about const float, I've never heard that before. So if I'm using a function that's not going to be altering the value of an argument in my function I use const before hand right? And if the function was going to alter the argument I would just leave it as float with nothing before it?

Well, it works now, and i think I understand what I did wrong :smile:
Thanks again.
 
Last edited:

1. What is a summation algorithm?

A summation algorithm is a mathematical procedure used to calculate the sum of a series of numbers. It involves adding each number in the series together to determine the total sum.

2. What are some common problems with summation algorithms?

One common problem with summation algorithms is the potential for numerical errors due to rounding or truncation. Another issue is the possibility of an infinite loop if the algorithm is not properly designed.

3. How can I improve the accuracy of my summation algorithm?

To improve accuracy, you can use a more precise data type such as a double or long double for storing the sum. You can also increase the number of iterations in the algorithm or use a more efficient summation method such as the Kahan summation algorithm.

4. What is the Kahan summation algorithm?

The Kahan summation algorithm is a more accurate method for calculating the sum of a series of numbers. It reduces the effects of rounding errors by keeping track of a separate variable for the error and adding it back to the sum at the end.

5. Are there any real-world applications of summation algorithms?

Yes, summation algorithms are commonly used in various fields such as statistics, physics, and finance. They are used to calculate the sum of data sets, determine the total energy of a system, and calculate the total value of investments, among other applications.

Similar threads

  • Programming and Computer Science
Replies
4
Views
710
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
9
Views
942
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
4
Views
880
Back
Top