[Haskell-cafe] rounding and undefined behavior

Matt Peddie mpeddie at gmail.com
Wed Feb 27 22:41:43 UTC 2019

Hi cafe,

It's been my general impression that when neither Haskell nor C
defines behavior in a particular situation, the behavior nonetheless
matches.  I was surprised to observe

    Prelude Data.Int> round  (4294967295 :: Double) :: Int16


    #include <stdio.h>
    #include <inttypes.h>
    int main(void) {
      double d = 4294967295;
      int16_t r = (int16_t) d;
      printf("%"PRId16"\n", r);
      return 0;

yields 0 when compiled and run.

As far as I can tell, neither language defines what this result should
be, so neither is doing anything wrong here.  But I was surprised that
they differ; does anyone know why Haskell's rounding operation behaves
the way it does (maybe there is some historical reason)?  Or can
someone perhaps point me to a standards document I missed that states
how the language must round out-of-bounds inputs?


Matt Peddie

More information about the Haskell-Cafe mailing list