Fix prelude definitions of abs/signum for Floats/Doubles

Shachaf Ben-Kiki shachaf at gmail.com
Thu Apr 11 11:58:58 CEST 2013


On Thu, Apr 11, 2013 at 2:50 AM, Levent Erkok <erkokl at gmail.com> wrote:
> Great catch! To be IEEE-754 compliant, signum *should* return NaN on NaN.
> Here's the new code:
>
> instance Num Float where
>     signum x | isNegativeZero x = x
>              | isNaN x          = x
>              | x == 0           = 0
>              | x > 0.0          = 1
>              | otherwise        = negate 1
>     abs x    | isNegativeZero x = 0
>              | x >= 0           = x
>              | otherwise        = negate x
>
> Of course, this extra test makes Simon's point even more relevant; i.e.;
> these should map to underlying CPU's native signum/abs functions if
> available.
>
> -Levent.
>
>

Maybe it would be simpler to say:

signum x | x == 0    = x
         | isNaN x   = x
         | x > 0     = 1
         | otherwise = negate 1

    Shachaf



More information about the Libraries mailing list