-2

Why am I getting the error: Floating point exception: 8

#include<stdio.h>
//grid problem
int fact(int n)
{

    int i,f=1;
    if(n==0)
        return 1;
    for(i=1;i<=n;i++)
        f*=i;
    return f;
}

int uniquePaths(int A, int B) {
    float m;
    m=fact(A+B-2)/(fact(A-1)*fact(B-1));
    return m;
}

int main(int argc, char const *argv[])
{
    int a,b;
    //aXb grid
    scanf("%d%d",&a,&b);
    printf("%d\n",uniquePaths(a,b) );
    return 0;
}
msc
  • 33,420
  • 29
  • 119
  • 214
  • I get no error with this. please verify your code – CIsForCookies Jun 07 '17 at 05:09
  • 3
    Show your input. – BLUEPIXY Jun 07 '17 at 05:10
  • 1
    `scanf("%d %d", &a, &b);` Note the whitespace. – cs95 Jun 07 '17 at 05:10
  • https://stackoverflow.com/questions/4236853/floating-point-exception-c-why-and-what-is-it – msc Jun 07 '17 at 05:15
  • https://stackoverflow.com/questions/31808661/why-am-i-getting-floating-point-exception-8 – msc Jun 07 '17 at 05:15
  • 2
    Probably it is estimated that overflow occurred in the calculation of a large factorial. So divide by 0 occurred. – BLUEPIXY Jun 07 '17 at 05:19
  • 2
    @Shiva: The white space in the format string is irrelevant; `%d` skips leading white space anyway. The spaces between the arguments are purely cosmetic. – Jonathan Leffler Jun 07 '17 at 06:29
  • These days, a 'floating point exception' almost invariably means 'integer divide by zero' — computations on floating point numbers generate infinities or NaN (not a number) values rather than a full floating point exception signal. So, look for reasons why you ended up dividing by zero. Your `uniquePaths()` function shouldn't assign to a `float` and then return an `int`. Note too that the arithmetic is done using integer arithmetic, not floating point arithmetic. All you are doing is losing precision (changing from 32-bit `int` to 23 bits of mantissa, and then converting back again). – Jonathan Leffler Jun 07 '17 at 06:32
  • Maybe [Why am I getting 'Floating point exception: 8'](https://stackoverflow.com/q/31808661/2410359) – chux - Reinstate Monica Jun 07 '17 at 14:09
  • 2
    With 32-bit `int`, any inputs `a,b` whose sum exceeds 64, will likely overflow the `fact(A-1)*fact(B-1)` with a UB result of 0. Then `int/int 0` causes the "Floating point exception". Posting inputs used would help clarify this post. – chux - Reinstate Monica Jun 07 '17 at 16:56

1 Answers1

1

If you add pre- and postconditions using the function assert you can make sure that parameters and function results have reasonable values:

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

//grid problem
int fact(int n)
{
    assert(n >= 0);
    int i, f = 1;
    if (n == 0) {
        return 1;
    }
    for (i = 1; i <= n; i++) {
        f *= i;
    }
    assert(f >= 1);
    return f;
}


int uniquePaths(int A, int B)
{
    assert(A >= 1);
    assert(B >= 1);
    int q = fact(A - 1) * fact(B - 1);
    assert(q > 0);
    int m = fact(A + B - 2) / q;
    assert(m >= 1);
    return m;
}


int main(int argc, char const *argv[])
{
    int a, b;
    //aXb grid
    int n = scanf("%d%d", &a, &b);
    if (n == 2) {
        printf("%d\n", uniquePaths(a, b));
    } else {
        fprintf(stderr, "invalid input\n");
        exit(1);
    }
    return 0;
}

On my machine, running the program above with intput 10 10, for instance, will result in

t: t.c:16: int fact(int): Assertion `f >= 1' failed.
Aborted

(I don't know why you get a floating point exception however.)

August Karlstrom
  • 10,773
  • 7
  • 38
  • 60
  • The test that is truly needed is if the product `(fact(A - 1) * fact(B - 1))` is zero. – chux - Reinstate Monica Jun 07 '17 at 14:11
  • @chux A factorial is always positive, also the postcondition asserts that is the case. – August Karlstrom Jun 07 '17 at 15:30
  • With an `int` quotient, the result near `INT_MAX`, that assigned to `float` could then get a rounded value of `INT_MAX+1`. Although this passes the `assert(m >= 1);`, the conversion back to `int` is `UB`, which includes a return value of 0. Even more of concern, the product `fact(A - 1) * fact(B - 1)` may still be 0 due to `int` overflow. The result of `int` OF is UB and a potential result is 0. Thus the suggestion of an `assert(product != 0)` just before the `int` division of a potential 0 that often signals itself as "Floating point exception". – chux - Reinstate Monica Jun 07 '17 at 16:45
  • 1
    @chux I missed the int to float conversion. I have changed float to int and added a zero assertion for the denominator. – August Karlstrom Jun 08 '17 at 14:17