Skip to content

Floating point exception

The slightly evil expression:

(-2147483648::Int) `div` (-1::Int)

Can produce floating point exceptions in GHCi:

$ ghci-6.6 
   ___         ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |      GHC Interactive, version 6.6, for Haskell 98.
/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|      Type :? for help.

Loading package base ... linking ... done.
Prelude> (-2147483648::Int) `div` (-1::Int)
zsh: floating point exception (core dumped)  ghci-6.6

Compiled:

$ cat A.hs
main = print $ (-2147483648::Int) `div` (-1::Int)

$ ghc A.hs

$ ./a.out 
zsh: floating point exception (core dumped)  ./a.out

Compiled -O:

$ ghc -O -no-recomp A.hs

$ ./a.out               
zsh: floating point exception (core dumped)  ./a.out

In Hugs:

Hugs.Base> (-2147483648::Int) `div` (-1::Int)
Unexpected signal

A C test cases:

$ cat t.c
#include <stdio.h>

int main() {
    printf("%d\n",((int)(-2147483648)) / ((int)(-1)));
    return 0;
}

$ gcc t.c
t.c: In function `main':
t.c:4: warning: this decimal constant is unsigned only in ISO C90
t.c:4: warning: integer overflow in expression

$ ./a.out 
-2147483648

Another:

$ cat t.c
#include <stdio.h>

main () {
    int i = -2147483648;
    int j = -1;
    int k = i / j;
    printf("%d\n", k);
}

$ gcc t.c
t.c: In function `main':
t.c:4: warning: this decimal constant is unsigned only in ISO C90

$ ./a.out 
zsh: floating point exception (core dumped)  ./a.out

What does H98 say about this?

-- Don

Trac metadata
Trac field Value
Version 6.6
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system Unknown
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information