C语言 浮点异常(核心转储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13664671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Floating point exception( core dump
提问by X_Trust
Program: So I made a program that take two numbers, N and L. N is the size of a 2D array and L is a number from 3 - 16. The program builds the array and starts at the center and works its way out in a counter clockwise spiral. I is the value of the center and its as you go through the array( in the spiral ) the value will increase by one. It it is prime, that number will be assigned to that spot and if not it *will take its place instead.
程序:所以我做了一个程序,它有两个数字,N 和 L。N 是一个二维数组的大小,L 是一个 3 - 16 的数字。程序构建数组并从中心开始,然后在逆时针螺旋。I 是中心的值,当您通过数组(在螺旋中)时,该值将增加一。如果它是素数,则该数字将被分配到该位置,如果不是,则*将代替它的位置。
Error: I'm getting a "Floating point exception " error, how would I solve this?
错误:我收到“浮点异常”错误,我该如何解决?
Code:
代码:
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 ;
}
回答by dreamcrash
You are getting Floating point exception because Number % i, when i is 0:
您收到浮点异常,因为 Number % i,当 i 为 0 时:
int Is_Prime( int Number ){
int i ;
for( i = 0 ; i < Number / 2 ; i++ ){
if( Number % i != 0 ) return -1 ;
}
return Number ;
}
Just start the loop at i = 2. Since i = 1 in Number % i it always be equal to zero, since Number is a int.
只需在 i = 2 处开始循环。由于 Number % i 中的 i = 1,因此它始终等于 0,因为 Number 是一个 int。
Btw: Mysticial did point out first on the comments.
顺便说一句:Mysticial 确实首先指出了评论。
回答by Amitesh Ranjan
Floating Point Exception happens because of an unexpected infinity or NaN. You can track that using gdb, which allows you to see what is going on inside your C program while it runs. For more details: https://www.cs.swarthmore.edu/~newhall/unixhelp/howto_gdb.php
由于意外的无穷大或 NaN,会发生浮点异常。您可以使用 gdb 跟踪它,它允许您查看 C 程序运行时内部发生的情况。更多详情:https: //www.cs.swarthmore.edu/~newhall/unixhelp/howto_gdb.php
In a nutshell, these commands might be useful...
简而言之,这些命令可能很有用......
gcc -g myprog.c
gcc -g myprog.c
gdb a.out
gdb a.out
gdb core a.out
gdb core a.out
ddd a.out
ddd a.out
回答by Vedant Jain
This is because, in the first iteration, Number is 0, and the code
这是因为,在第一次迭代中, Number 为0,而代码
Number % i
yields an infinite value, and to avoid errors the compiler handles it in Exception Handling, giving you this runtime error.
产生一个无限值,为了避免错误,编译器在异常处理中处理它,给你这个运行时错误。
To avoid this, just run a 2 - indexed for loop.
为避免这种情况,只需运行一个 2 索引的 for 循环。

