[Haskell-cafe] Re: Desired behaviour of rounding etc.

Henning Thielemann schlepptop at henning-thielemann.de
Fri Oct 8 09:47:37 EDT 2010

Daniel Fischer schrieb:
> The methods of the RealFrac class produce garbage when the value lies 
> outside the range of the target type, e.g.
> Prelude GHC.Float> truncate 1.234e11 :: Int  -- 32-bits
> -1154051584
> and, in the case of truncate, different garbage when the rewrite rule 
> fires:
> Prelude GHC.Float> double2Int 1.234e11
> -2147483648
> I'm currently working on faster implementations of properFraction, 
> truncate, round, ceiling and floor for Float and Double, so I'd like to 
> know
> - does it matter at all what garbage is returned in the above case?
> - if it does, what is the desired behaviour (at least for Int, I can't 
> cater for all possibilities)?

For me the proper definition of truncate is:

  If x lies outside the range of Int, then (truncate x) is undefined.

Silently replacing explicit undefined with garbage would keep all
correct programs correct and would only change the way in which wrong
programs fail.

More information about the Haskell-Cafe mailing list