Fix prelude definitions of abs/signum for Floats/Doubles

Herbert Valerio Riedel hvr at gnu.org
Wed Apr 10 22:56:54 CEST 2013


Levent Erkok <erkokl at gmail.com> writes:

[...]

> Proposed fix: Section 6.4.4 of the report
> http://www.haskell.org/onlinereport/basic.html#sect6.4 gives "default"
> definitions for abs/signum; which fails to take into account of
> negative-zero values for floating-point types. An easy fix would be to add
> to the report a note on the status of negative-zero for Real/Float
> instances; with individual implementations explicitly checking for
> negative-0 first. For instance GHC's implementation can be changed as
> follows:
>
> instance Num Float where
>     signum x | isNegativeZero x = x
>              | x == 0           = 0
>              | x > 0.0          = 1
>              | otherwise        = negate 1
>     abs x    | isNegativeZero x = 0
>              | x >= 0           = x
>              | otherwise        = negate x
>
> A similar change would need to be done for the "Num Double" instance as
> well. Of course, implementations can take advantage of the underlying CPU's
> native floating-point abs/sign functions if available as well, avoiding
> explicit tests at the Haskell code; based on the underlying platform.

+1



More information about the Libraries mailing list