C语言 C 编程 - 浮点异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4122172/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:56:47  来源:igfitidea点击:

C programming - floating point exception

c

提问by matthewmpp

matthewmpp@annrogers:~/Programming/C.progs/Personal$ cat prime4.c
/*
 * File:   main.c
 * Author: matthewmpp
 *
 * Created on November 7, 2010, 2:16 PM
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

/*
prime numbers.
version4
should tell whether a number is prime or not prime.
by using other prime numbers.
 */

int input_func ()
{
    char line[100];
    int n_input;

    while (1) {
        printf("Please enter a whole number.\n");
        fgets(line, sizeof (line), stdin);
        sscanf(line, "%d", &n_input);

        if (n_input >= 0)
            break;

        return (n_input);
    }
}

int ifstatements_func (n_ifstate)
int n_ifstate;
{
    if (n_ifstate == 0) {
        printf("The number, %d, is not prime and has no factors.\n", n_ifstate);
        exit(1);
    }

    if (n_ifstate == 1) {
        printf("The number, %d, is not prime.\n", n_ifstate);
        printf("The factors of %d, is %d.\n", n_ifstate, n_ifstate);
        exit(1);
    }

    if (n_ifstate == 2) {
        printf("The number, %d, is a prime.\n", n_ifstate);
        printf("The factors of %d, are 1 and %d.\n", n_ifstate, n_ifstate);
        exit(1);
    }
    if (n_ifstate == 3) {
        printf("The number, %d, is a prime.\n", n_ifstate);
        printf("The factors of %d, are 1 and %d.\n", n_ifstate, n_ifstate);
        exit(1);
    }
    return (n_ifstate);
}

int square_root_func (n_prmfnc)
int n_prmfnc;
{
    int i;  //counter

    float sq_root_f;
    int sq_root_i;

    int primes[100];
    int length_primes;

    primes[0] = 2; /*first prime is 2.*/
    primes[1] = 3; /*second prime is 3.*/
    length_primes = sizeof (primes);

    //printf ("before.sq_root.value of n_prmfnc=%d\n", n_prmfnc);
    sq_root_f = sqrt(n_prmfnc);
    sq_root_i = sq_root_f;
    //printf ("prmfnc.after.sq_root\n");
    //printf ("value of sq_root=%.3f\n", sq_root_f);
    //printf ("value of sq_root=%d\n", sq_root_i);

    return (sq_root_i);
}

int prime_func (sq_root_pf, n_pf)
int sq_root_pf, n_pf;
{
    int prime_counter;
    int prime_temp;
    int prime_flag=0;

    int primes_pf[100];
    int i;                  //counter

    primes_pf[0]=2;
    primes_pf[1]=3;
    primes_pf[2]=5;

    printf ("before.for.in.pf");
    for (i = 0; i <= 100; ++i) {
        printf ("after.for.in.pf");
        if (primes_pf[i] <= sq_root_pf) {
            printf ("before.modulus.in.pf");
            prime_temp = n_pf % primes_pf[i];
            printf ("after.modulus.in.pf");
            if (prime_temp == 0) {
                ++prime_counter;
                if (prime_counter == 0)
                    prime_flag = 1; /*yes, number is prime.*/
            }
        }
    }
    return (prime_flag);
}

int main() {
    int n_main1;    //number from input
    int n_main2;    //number after if statements
    int sq_root_main;    //square root of number from function
    int prime_flag_main;   //value of 1 if it is a prime

    n_main1 = input_func ();
    printf("main.after.input.function=%d.\n", n_main1);

    n_main2 = ifstatements_func (n_main1);
    printf ("main.after.ifstatments.function=%d\n", n_main2);

    sq_root_main = square_root_func (n_main2);
    printf ("main.after.square_root_func_func=%d\n", sq_root_main);

    prime_flag_main = prime_func (sq_root_main, n_main2);
    printf ("main.after.prime_func=%d\n", prime_flag_main);

    return (EXIT_SUCCESS);
}


OUTPUT:
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -c prime4.c
matthewmpp@annrogers:~/Programming/C.progs/Personal$ cc -o prime4 prime4.c -lm
matthewmpp@annrogers:~/Programming/C.progs/Personal$ ./prime4
Please enter a whole number.
44
main.after.input.function=44.
main.after.ifstatments.function=44
main.after.square_root_func_func=6
Floating point exception
matthewmpp@annrogers:~/Programming/C.progs/Personal$ 

STATEMENT:the error is in the prime_func. I believe the cause is the modulus (% sign).

声明:错误在 prime_func 中。我相信原因是模数(%符号)。

QUESTION:why am I getting the Floating Point Exception and how do I fix it?

问题:为什么我会收到浮点异常以及如何修复它?

回答by Sven Marnach

What happens is a division by zero. You only initialise the first three entries of primes_pf, but iterate over all of them (actually, your loop runs even one past the last entry; use i < 100instead of i <= 100to fix this). For all but the first three entries, you divide by some unitialised quantity, and one of the entries apparently happens to be zero. Don't use unitialised values.

发生的是除以零。您只初始化 的前三个条目primes_pf,但遍历所有条目(实际上,您的循环甚至运行到最后一个条目之后;使用i < 100代替i <= 100来解决此问题)。对于除前三个条目之外的所有条目,您除以某个统一化的数量,其中一个条目显然恰好为零。不要使用单元化的值。

回答by R.. GitHub STOP HELPING ICE

"Floating point exception" is a misnomer. It only happens on integerdivision by zero and a few other division-related operations.

“浮点异常”是用词不当。它只发生在整数除以零和一些其他与除法相关的操作上。

回答by cflute

Not sure I believe the answer above!

不确定我相信上面的答案!

X = 5.0; Y = 0.0; Z = X/Y;

X = 5.0; Y = 0.0; Z = X/Y;

This will give a floating point exception....

这将给出一个浮点异常....

The problem would appear to be that prime_pf is only initialised for 3 elements. So the modulo is attempting to divide by zero. BTW, if you add \n to your printf statement, and add the extra statement fflush(stdout); you are more likely to see the debug output before the program errors.

问题似乎是 prime_pf 仅针对 3 个元素进行了初始化。所以模数试图除以零。顺便说一句,如果您将 \n 添加到 printf 语句中,并添加额外的语句 fflush(stdout); 您更有可能在程序错误之前看到调试输出。

回答by SiegeX

The problem exists in your primes_pfvariable. You seemed to have initialized the first three elements of this integer array, but when iterator igoes beyond 2, primes_pf[i]is reading from uninitialized memory and getting compared to sq_root_pf; that can't be right.

问题存在于您的primes_pf变量中。您似乎已经初始化了这个整数数组的前三个元素,但是当迭代器i超过 2 时,primes_pf[i]正在从未初始化的内存中读取并与sq_root_pf;进行比较。那不可能是对的。

I haven't taken the time to fully understand your algorithm but my best guess is that you forgot to assign a new value to primes_pfsomewhere in your for loop.

我没有花时间完全理解你的算法,但我最好的猜测是你忘记为primes_pf你的 for 循环中的某个地方分配一个新值。

回答by Arif Burhan

sqrt(x) needs x to be of type double, you have used int.

Cast to double (double)n_prmfnc

转换为双(double)n_prmfnc

I am VERYsure about this !

非常确定这一点!