简体   繁体   English

我的练习浮点异常(C语言)

[英]Floating Point Exception of my Excercise (C language)

I've just made a program that implements a binomial function (n!/k!*(nk)!) . 我刚刚制作了一个实现二项式函数(n!/k!*(nk)!)

I can compile my program without any problems but when i scanf the 2 int (n and k), it says "Floating Point Exception". 我可以编译程序,没有任何问题,但是当我扫描2 int(n和k)时,它显示“ Floating Point Exception”。

I've tried many ways to fix it but i can't find the problem because i'm not good with c programming :( I'm learning. Someone can help me please? Thank you very much. 我已经尝试了多种方法来解决它,但是我找不到问题,因为我对c编程不满意:(我正在学习。有人可以帮助我吗?非常感谢。

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

int factorial( int n ){
    int result;
    if( n == 0 ){
        result = 0;
    } else {
        result = n * factorial((n - 1));
    }
    return result;
}

char *stringa_binomiale(int n, int k){

    char *s;
    s=malloc(sizeof(char)*20);

    int b;

    b = factorial(n)/(factorial(k)*factorial(n-k));

    sprintf(s,"%i su %i fa %i",n ,k ,b);

    return s;
}

int main (void){

    int n;
    int k;
    char *s;
    s=malloc(sizeof(char)*20);

    printf("n:");
    scanf("%i",&n);
    printf("k:");
    scanf("%i",&k);

    s= stringa_binomiale(n,k);
    printf("%s \n", stringa_binomiale(n, k));

    free(s);
    return 0;
}

Your factorial function always returns 0 for any input, because the base case is wrong. 您的factorial函数对于任何输入总是返回0 ,因为基本情况是错误的。 Your base case returns 0 , and when you multiply by this you get 0 . 您的基本情况返回0 ,乘以该值便得到0

As a result, you're dividing by 0 in stringa_binomiale , which causes a floating point exception. 结果,您要在stringa_binomiale除以0 ,这将导致浮点异常。

The base case should be n == 1 and it should return 1 . 基本情况应为n == 1 ,并且应返回1

What could give an FPE? 什么可以提供FPE? :) :)

HINT: try to print the outputs of your "factorial" function.. 提示:尝试打印“阶乘”函数的输出。

SOLUTION: the "factorial" function always returns 0, and its return is used in a division. 解决方案:“阶乘”函数始终返回0,并且其返回值用于除法运算。 n! n! is 1 if n is 0: change that line and you're fine. 如果n为0,则为1:更改该行即可。

your factorial function was always returning 0 and thus dividing by 0 was giving floating point exception 您的factorial函数始终返回0 ,因此除以0会给出floating point exception

the factorial function should be factorial函数应为

int factorial( int n ) {
  int result;
  if( n == 0 ){
    result = 1;
  } else {
    result = n * factorial((n - 1));
  }
  return result;
 }

and here is the link http://ideone.com/CTKDyX .. 这是链接http://ideone.com/CTKDyX ..

An integer division by zero also gets report as "floating point exception". 整数除以零也会得到报告为“浮点异常”。 Don't know why that is, there probably are historical reasons. 不知道为什么,可能有历史原因。

If your C compiler is recent enough, you should compile with "-fsanitize=undefined". 如果您的C编译器足够新,则应使用“ -fsanitize = undefined”进行编译。 You'll get runtime messages instead of weird behavior for a lot of C errors, including this one. 对于很多C错误(包括此错误),您将获得运行时消息,而不是奇怪的行为。

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

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