nasso
nasso

Reputation: 605

-2147483648 % -1 causes floating point exception

For example:

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

int main(int argc, char **argv)
{
    int a = (int) strtol(argv[1], (char**) NULL, 10);
    int b = (int) strtol(argv[2], (char**) NULL, 10);

    printf("%d %% %d = %d\n", a, b, a % b);
    return (0);
}

(the program takes the values as command line arguments because when hard-coded I think -2147483648 % -1 gets replaced with 0 at compile time)

Running this simple program with -2147483648 and -1 as the first and second argument respectively raises a SIGFPE. Why does this happen? Is there other special cases like it? How can I avoid it?

Thanks!

Upvotes: 0

Views: 53

Answers (0)

Related Questions