简体   繁体   English

浮点异常错误

[英]Floating Point Exception Error

This program compiles fine, but it returns a message "Floating Point Exception" when I run it. 这个程序编译得很好,但是当我运行它时会返回一条消息“浮点异常”。 I've looked at other threads and the problem appears to be dividing by 0, but I have looked over the program and there's no division by zero in my entire program. 我看了其他线程,问题似乎是除以0,但我查看了程序,并且在整个程序中没有除零。 I even used the absolute value function in case. 我甚至使用绝对值函数以防万一。

By the way, the program is meant to reduce fractions. 顺便说一下,该程序旨在减少分数。

Example input: 6 12 , representing the fraction 6/12 示例输入: 6 12 ,表示分数6/12
Expected output: 1/2 预期产量: 1/2

#include <stdio.h>

/*declaring variables*/
int num1, num2, num1b, num2b, gcd, x;
int higher, lower, higher_2, lower_2;

/*declaring functions*/
int find_gcd(int num1, int num2);
void reduce(int numerator, int denominator, int *reduced_numerator, int *reduced_denominator);

int main(void)
{
    do
    {
        printf("enter 2 numbers:  ");
        scanf("%d %d", &num1, &num2);
        reduce(higher, lower, &higher_2, &lower_2);
        printf("enter 0 to end program and any number continue: \n");
        scanf("%d", &x);
    } while(x != 0);

    return 0;
}

void reduce(int numerator, int denominator, int *reduced_numerator, int *reduced_denominator)
{
    num1=numerator;
    num2=denominator;

    gcd =find_gcd(numerator, denominator);

    *reduced_numerator = (numerator/abs(gcd));
    *reduced_denominator = (denominator/abs(gcd));

    printf("The GCD is %d/%d\n", *reduced_numerator, *reduced_denominator); 
}

int find_gcd(int m, int n)
{
    while (n != 0) {
        int remainder = m % n;
        m = n;
        n = remainder;
    }
    return m;
}

Your main problem is that you are not passing your input values num1 and num2 into your reduce() function. 您的主要问题是您没有将输入值num1num2传递给reduce()函数。 Instead you are passing in the global variables higher and lower . 相反,你传递的是higherlower的全局变量。 You didn't assign any values to them, but global variables are always initialized to 0 by default. 您没有为它们分配任何值,但默认情况下全局变量始终初始化为0。 Therfore, you run into the exception, because in reduce() you divide 0 by 0. You can verify that with a debugger. 因此,您遇到异常,因为在reduce()您将0除以0.您可以使用调试器验证。

If I change your main() as follows, then your code is at least working for your test case with 6 and 12 as input: 如果我按如下方式更改main() ,那么您的代码至少适用于您的测试用例,输入为612

int main(void)
{
    do
    {
        printf("enter 2 numbers:  ");
        scanf("%d %d", &num1, &num2);
        reduce(num1, num2, &higher_2, &lower_2);
        printf("enter 0 to end program and any number continue: \n");
        scanf("%d", &x);
    } while(x != 0);

    return 0;
}

Output: 输出:

enter 2 numbers: 6 输入2个数字:6
12 12
The GCD is 1/2 GCD是1/2
enter 0 to end program and any number continue: 输入0结束程序,任何数字继续:


As indicated in the comments you should also get rid of global and spurious variables. 如评论中所示,您还应该摆脱全局和虚假变量。 Therefore, you should first delete the following lines in your code: 因此,您应该首先删除代码中的以下行:

/*declaring variables*/
int num1, num2, num1b, num2b, gcd, x;
int higher, lower, higher_2, lower_2;

Then let your main() function start the following way: 然后让main()函数以下列方式启动:

int main(void)
{
    int num1, num2, higher_2, lower_2, x;
    ...
}

And your reduce() function should read like this: 你的reduce()函数应如下所示:

void reduce(int numerator, int denominator, int *reduced_numerator, int *reduced_denominator)
{
    int gcd = find_gcd(numerator, denominator);

    *reduced_numerator = (numerator/abs(gcd));
    *reduced_denominator = (denominator/abs(gcd));

    printf("The GCD is %d/%d\n", *reduced_numerator, *reduced_denominator); 
}

So far, you don't use your variables higher_2 and lower_2 in the main() function, but I guess you plan to do so. 到目前为止,你没有在main()函数中使用变量higher_2lower_2 ,但我想你打算这样做。 If not, you can also get rid of them together with parameters 3 and 4 of your reduce() function. 如果没有,您也可以将它们与reduce()函数的参数3和4一起删除。


There is another issue with the code you provided (thanks to @user3629249 for pointing it out): You are missing an include for the abs() function. 您提供的代码还有另一个问题(感谢@ user3629249指出):您缺少abs()函数的include。 So you need to add the line #include <stdlib.h> at the beginning of your code ( include <math.h> will also so the trick, as well as include <Windows.h> on Windows). 因此,您需要在代码的开头添加#include <stdlib.h>行( include <math.h>也是如此,以及在Windows上include <Windows.h> )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM