Skip to content

Instantly share code, notes, and snippets.

@chris-taylor
Created May 1, 2012 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-taylor/2568426 to your computer and use it in GitHub Desktop.
Save chris-taylor/2568426 to your computer and use it in GitHub Desktop.
alternative implementation of floating point abs and num
data NewFloat a = F a deriving (Eq,Ord,Show)
instance RealFloat a => Num (NewFloat a) where
F x + F y = F (x + y)
F x - F y = F (x - y)
F x * F y = F (x * y)
fromInteger n = F (fromInteger n)
abs = newAbs
signum = newSignum
newAbs (F x)
| x > 0 = F x
| x == 0 = F 0
| x < 0 = F (-x)
newSignum (F x)
| x > 0 = F 1
| x == 0 = if isNegativeZero x
then F (-0.0)
else F 0
| x < 0 = F (-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment