Search code examples
cfloating-point-exceptions

Why is this generating Floating point exception?


This is the second example of wikipedia SIGFPE page.

#include <limits.h>
int main(void)
{
    volatile int x=INT_MIN;
    volatile int y=-1;
    x=x/y;
    return 0;
}

It is inverting the sign to positive of INT_MIN. How can it be FPE?


Solution

  • The Wikipedia article answers:

    ... triggers the signal because the quotient, a positive number, is not representable.

    INT_MIN / -1 = -INT_MIN
                 = INT_MAX + 1
                 => invalid number
                 => floating point exception (FPE)