. Advertisement .
..3..
. Advertisement .
..4..
Recently, I ran some of my p code, and it gave the warning text:
Floating point exception
While searching, I realized that some people added some command lines in my sample above. But I don’t think it is the best way to correct the problem – floating point exception (core dumped) How would you explain this trouble? or Is there a better way? Below is the detail of the command that I used
void Array_Loop( int *Array, int n, int L ) ;
int Is_Prime( int Number ) ;
int main( int argc, char *argv[] ){
int **Array ;
int n, L ;
n = atoi( argv[1] ) ;
L = atoi( argv[2] ) ;
Matrix_Build( &Array, n, n ) ;
Array_Loop( Array, n, L ) ;
return 0 ;
}
void Array_Loop( int *Array, int n, int L ){
int i, j, k, h ;
int lctn, move;
lctn = n / 2 + 1 ;
i = lctn ;
j = lctn ;
move = 1
while( i != 0 && j != n ){
for( j = lctn ; j < lctn + move ; j++ ){
if( L % 2 == 2) Array[i][j] = -1 ;
else Array[i][j] = Is_Prime( L ) ;
L++ ;
}
move = move * -1 ;
for( i = i ; i > lctn - move ; i-- ){
if( L % 2 == 2) Array[i][j] = -1 ;
else Array[i][j] = Is_Prime( L ) ;
L++ ;
}
move-- ;
for( j = j ; j > lctn - move ; j-- ){
if( L % 2 == 2) Array[i][j] = -1 ;
else Array[i][j] = Is_Prime( L ) ;
L++ ;
}
move = move * -1 ;
for( i = i ; i < lctn - move ; i-- ){
if( L % 2 == 2) Array[i][j] = -1 ;
else Array[i][j] = Is_Prime( L ) ;
L++ ;
}
move++ ;
}
}
int Is_Prime( int Number ){
int i ;
for( i = 0 ; i < Number / 2 ; i++ ){
if( Number % i != 0 ) return -1 ;
}
return Number ;
}
The cause: You are receiving Floating point exception due to
Number % i
0
Solution: Start the loop at
i = 2
. It will always equal zero sincei = 1
isNumber % i
, because Number is an int.Unexpected infinity or NaN can cause Floating Point Exception. You can monitor that with gdb. This allows you to track what’s happening inside your C program as it runs. For more details: https://www.cs.swarthmore.edu/~newhall/unixhelp/howto_gdb.php
These commands may be of some use in a nutshell.
gcc -g myprog.c
gdb a.out
gdb core a.out
ddd a.out