Floating Point Exception...

Greetings

Again I've got a problem with my Project. Sometimes it returns a Floating Point Exception, but I don't know why.

I suppose, this is the part causing the Exception:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
		if(moveplayerp==0 && playerlock==0 && pos!=1){
			xold=my_data->posx;
			yold=my_data->posy;
					
			dist=floor(sqrt((pow((ballx-xold),2)+pow((bally-yold),2))));
			int aura=my_data->aura;
			int mysize=my_data->grosse;
			int ballz_calc=ballz*100;
			
			int tor;
			if(side==1){
				// spiele von links nach rechts
				tor=100;
			}else if(side==2){
				tor=0;
			}
			int disttor=floor(sqrt(pow((tor-xold),2)));
			xmin=my_data->xmin;
			xmax=my_data->xmax;
			ymin=my_data->ymin;
			ymax=my_data->ymax;
			
			if((dist<=aura && ((ballown!=mypid && ballteam!=playerteam) || ballteam==0 || ballown==0)) && ballz_calc<=mysize && balllock==0){
				if(ballteam==0){
					printf("(%d<=%d && %d!=%d && %d!=%d) [erobern?]\n",dist,aura,ballown,mypid,ballteam,playerteam);
				}
				// Ball erobern
				int spielint=my_data->spielint;
				int schnell=my_data->schnell;
				int annahme=(spielint+schnell)/20;
				if(annahme==0){annahme=1;}
				int zeit=ceil(dist/annahme);
				if(zeit<=1){zeit=1;}
				//printf("ball erobern %d\n", zeit);
				sleep(zeit);
				if(ballown==0 && balllock==0){
					printf("%s hat den Ball [%d]\n",my_data->name,playerteam);
					ballz=0;
					my_data->hasball=1;
					ballown=mypid;
					ballteam=playerteam;
					int posx=my_data->posx;
					int posy=my_data->posy;
					int kondition=my_data->kondition;
					int frische=floor((my_data->frische)/100);
					int schnell=my_data->schnell;
					int movetox, movetoy;
					if(side==1){
						//spiele von links nach rechts
						movetox=((posx)+((schnell+kondition+frische)/30));
					}else if(side==2){
						// Spiele von links nach rechts
						movetox=((posx)-((schnell+kondition+frische)/30));
					}
					if(my_data->posy<29){
						movetoy=((posy)+((schnell+kondition+frische)/30));
					}else if(my_data->posy>36){
						movetoy=((posy)-((schnell+kondition+frische)/30));
					}else{
						movetoy=posy;		
					}
					my_data->movetox=movetox;
					my_data->movetoy=movetoy;
					moveplayer(threadarg);
				}
				else if((ballown!=mypid && ballteam!=playerteam && balllock==0)){
					// Zweikampf
					//printf("Zweikampf nötig\n");
					int hisnum;
					t=0;
					int olddist=0, getdist;
					struct player_data *the_data;
					while(t<NUM_THREADS){
						the_data = (struct player_data *) &player[t];
						int histeam=the_data->team;
						int heball=the_data->hasball;
						int hispid=the_data->pid;
						if(histeam!=playerteam && hispid!=mypid && heball==1){
							int hisx=the_data->posx;
							int hisy=the_data->posy;
							getdist=sqrt((xold-hisx)*(xold-hisx) + (yold-hisy)*(yold-hisy));
							//printf("%d<%d\n",getdist,olddist);
							if(getdist<=aura){
								hisnum=t;
							}
						}
						t++;
					}
					the_data = (struct player_data *) &player[hisnum];
					int histeam=the_data->team;
					the_data = (struct player_data *) &player[mynum];
					int myteam=the_data->team;
					if(histeam!=myteam){
						// Zweikampf zwischen player[mynum] und player[hisnum]
						//int zweik=zweikampf(&player[mynum], &player[hisnum]);
						zweikampfen[0].pl1=mynum;
						zweikampfen[0].pl2=hisnum;
						if(eventlock==0){
							int rc = pthread_create(&threads2[0], &attr, zweik, (void *) &zweikampfen[0]);
						}
						
					}
				}
			}


This is moveplayer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
	char *hello_msg;
	struct player_data *my_data;
	my_data = (struct player_data *) threadarg;
		
	hello_msg = my_data->name;
	int mypid=my_data->pid;
	int mypos=my_data->pos;
	
	int schnell = my_data->schnell;
	int frische = floor(my_data->frische)/100;
	int xold=my_data->posx;
	int rundist=my_data->rundist;
	if(xold>100){xold=100;my_data->posx=100;}
	else if(xold<0){xold=0;my_data->posx=0;}
	
	int yold=my_data->posy;
	if(yold>64){yold=64;my_data->posy=64;}
	else if(yold<0){yold=0;my_data->posy=0;}
	
	int x = my_data->movetox;
	if(x>100){x=100;my_data->movetox=100;}
	else if(x<0){x=0;my_data->movetox=0;}
	
	int y = my_data->movetoy;
		
	int xmin=my_data->xmin;
	int xmax=my_data->xmax;
	int ymin=my_data->ymin;
	int ymax=my_data->ymax;
	int xnew, ynew;
	if(xmin<0){my_data->xmin=0;xmin=0;}
	else if(xmax>100){my_data->xmax=100;xmax=80;}
	if(ymin<0){my_data->ymin=0;ymin=0;}
	else if(ymax>64){my_data->ymax=64;ymax=64;}

	// X
	if((x==ballx && (ballx<=(x-15) || ballx>=(x+15)) && (ballx>=0 && ballx<=100)) && ballown==0 && mypos!=1){
		/* kein Eingriff, wenn nur Ball geholt wird */
	}else{		
		if(x<xmin){x=xmin;}
		else if(x>xmax){x=xmax;}
	}
	// Y
	if((y==bally && (bally<=(y-15) || bally>=(y+15)) && (bally>=0 && bally<=64)) && ballown==0 && mypos!=1){
		/* kein Eingriff, wenn nur Ball geholt wird */
	}else{
		if(y<ymin){y=ymin;}
		else if(y>ymax){y=ymax;}
	}
	
	if(ballown==0 && ballteam==0 && minute>1){
		printf("movetoy: (%d|%d) but ball: (%d|%d) AND [%d<%d|%d<%d] \n",x,y,ballx,bally,x,xmax,y,ymax);
	}
	/*if(y>64){y=64;my_data->movetoy=64;}
	else if(y<0){y=0;my_data->movetoy=0;}*/
	
	int hasball = my_data->hasball;
	//printf("%d,%d (%d,%d)\n",x,y,xold,yold);
	int dist=ceil(sqrt(pow(x-xold,2)+pow((y-yold),2)));
	//printf("dist to target: %d\n",dist);
	if(dist!=0){
		my_data->moveplayerp=1;
		int geschw = schnell/10;
		if(geschw==0){geschw=1;}
		int time=ceil(dist/geschw);
		if(time<=0){time=1;}
		//printf("time to target: %d\n",time);
		if(hasball==1 && ballown==mypid && balllock==0){
			int xcalc=ceil((x-xold)/time);
			int ycalc=ceil((y-yold)/time);
			int t=0;
			// Ballbesitz zurück
			while(t<time){
				//bewege den Ball
				sleep(1);
				ballx=ballx+xcalc;
				bally=bally+ycalc;
				t++;
			}
			ballx=x;
			bally=y;
		}else{
			sleep(time);
		}
		my_data->posx=x;
		my_data->posy=y;
		int testx=my_data->posx;
		int testy=my_data->posy;
		my_data->rundist=rundist+dist;
		//if(testx==x){printf("x stimmt");}
		//if(testy==y){printf("y stimmt");}
			
		my_data->moveplayerp=0;		
	}



Usually after Printing "Boris hat den Ball [1]" the Program aborts with the Floating Point Exception. I don't think it is because of a corrupt mysql row, because it worked before...

Anyone got an Idea or a hint?

Thanks beforehand
Your best bet is to run this in a debugger to see what is causing the exception. My guess is that something like movetox=((posx)+((schnell+kondition+frische)/30)); is returning a value that it doesn't like (e.g too small/large for a float to store).
Your best bet is to run this in a debugger to see what is causing the exception.

What should I look out for in gdb?

My guess is that something like movetox=((posx)+((schnell+kondition+frische)/30)); is returning a value that it doesn't like (e.g too small/large for a float to store).

Because of the given Data-Limits we have a range of 0 to 110. That should not be a problem, but i've changed something with the value of "frische" maybe you are right.
I'll try if
int frische = floor((my_data->frische)/100);
makes any difference to the current line
int frische = floor(my_data->frische)/100;
Last edited on
Ok, i've got a result with gdb

[Thread 0xa726bb90 (LWP 27081) exited]

Program received signal SIGFPE, Arithmetic exception.
[Switching to Thread 0xad277b90 (LWP 26770)]
0x08050b9f in poschecks ()
(gdb) bt
#0 0x08050b9f in poschecks ()
#1 0xb7afe4fb in start_thread () from /lib/i686/cmov/libpthread.so.0
#2 0xb7be7d7e in clone () from /lib/i686/cmov/libc.so.6

EDIT:
Another process:

[Thread 0x9e093b90 (LWP 27436) exited]

Program received signal SIGFPE, Arithmetic exception.
[Switching to Thread 0xb2abcb90 (LWP 26460)]
0x08050b9f in poschecks ()
(gdb) bt
#0 0x08050b9f in poschecks ()
#1 0xb7b384fb in start_thread () from /lib/i686/cmov/libpthread.so.0
#2 0xb7c21d7e in clone () from /lib/i686/cmov/libc.so.6
(gdb)


How can I find out wich var was 0x08050b9f?

EDIT (II):
0x08050b9f had value $1 = 134548383

Last edited on
Start putting in some breakpoints to isolate the section of code that is causing you the problem.
Is there no easyer way? Adding breakpoints is a bit ardous when this routine runs about 40 times a minute for each of the 22 pthreads...
This is kind of the nature of runtime errors/exceptions, and the purpose of a debugger.

You really have 2 main ways to debug the code.

1) use a debugger. Add breakpoints, setup watches and triggers (if needed). This will allow you to follow the code through and keep an eye on the variables.

2) Add some exception handling to your code. Start adding Try/Catch blocks around pieces of code that you suspect maybe causing an exception. When you hit the piece of code causing the exception you can then start looking at why, add some cout statements etc to get more information about the variables at the time of execution etc.

There really is no magic answer to runtime errors.

Just a note, printf is a C function. You should be using cout etc and IOStreams for C++ code.


Note: Even at this point in time I am debugging code to find an intermittent runtime error that is caused by a variation in packet structure within a data protocol =\ Again, I wish there was a magic fix that doesn't involve me analyzing individual bytes.
In Case someone got the same problem:

It was something inside the function/if-clause i've posted. After Compiling with the additional option "-g" the program informed me, wich line might be the root of the Exception. (Also it informs you if the sourcefile was edited in the meantime)

So I could find out what it was.

int schnell=my_data->schnell;

This was declared twice and changed the value uf "schnell" to something like 17 mio.
After removing the second declaration it worked fine
On the upside. You have learnt some valuable skills in debugging a runtime exception. Something you will do many many many times in your SW development life.

The more proficient you can be at runtime debugging, the better software developer you will become too. :)
Topic archived. No new replies allowed.