Skip to content

Fix definitions of abs/signum for Floats/Doubles

The current definition of abs doesn't work correctly for -0.0 (negative zero); and the signum function doesn't return NaN on NaN. The issue is discussed in detail in the following thread:

http://www.haskell.org/pipermail/libraries/2013-April/019677.html

To summarize, the proposal is to change both the Float and Double instance definitions for signum/abs as follows:

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

Simon PJ expressed interest in taking advantage of the underlying platforms floating-point instructions when available to speed things up, and the above referenced thread has some discussion regarding how this might be done. However, it might be best to start with a "correct" implementation first, and then later worry about speed.

On a related note, this "bug" is actually present in the Haskell'98 standard definition itself, which seems to have overlooked NaN's and negative-0's. It'd be great if the standard got a similar amendment as well.

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