2 - Copyright (C) 2009-2010 Nick Bowler.
4 - License BSD2: 2-clause BSD license. See LICENSE for full terms.
5 - This is free software: you are free to change and redistribute it.
6 - There is NO WARRANTY, to the extent permitted by law.
9 -- | Definition of the core floating point types and basic manipulation of
11 {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, OverlappingInstances #-}
12 {-# LANGUAGE MagicHash #-}
13 module Data
.Floating
.Types
(
14 Double(..), Float(..), FloatConvert
(..), Complex(..)
17 import Prelude
hiding (Double, Float)
18 import qualified GHC
.Exts
as GHC
23 import Data
.Floating
.Classes
28 -- | The Double type. This is expected to be an identical declaration to
29 -- the one found in GHC.Prim. We avoid simply using GHC's type because we need
30 -- to define our own class instances.
31 data Double = D
# Double#
34 data Float = F
# Float#
38 data (PrimFloat a
) => Complex a
= !a
:+ !a
41 -- | Coercion to floating point types.
42 class FloatConvert a b
where
43 -- | Convert to a floating point type. Conversions from integers and real
44 -- types are provided, as well as conversions between floating point types.
45 -- Conversions between floating point types preserve infinities, negative
49 instance FloatConvert
Double CDouble
where
50 toFloating
= unsafeCoerce
52 instance FloatConvert CDouble
Double where
53 toFloating
= unsafeCoerce
55 instance FloatConvert
Float CFloat
where
56 toFloating
= unsafeCoerce
58 instance FloatConvert CFloat
Float where
59 toFloating
= unsafeCoerce
61 instance FloatConvert
Double Float where
62 toFloating
(D
# x
) = F
# (double2Float
# x
)
64 instance FloatConvert
Float Double where
65 toFloating
(F
# x
) = D
# (float2Double
# x
)
67 instance FloatConvert
Integer Double where
68 toFloating x
= D
# (doubleFromInteger x
)
70 instance FloatConvert
Integer Float where
71 toFloating x
= F
# (floatFromInteger x
)
73 instance Real a
=> FloatConvert a
Double where
74 toFloating x
= D
# (num
/## denom
) where
75 !(D
# num
) = toFloating
. numerator . toRational $ x
76 !(D
# denom
) = toFloating
. denominator . toRational $ x
78 instance Real a
=> FloatConvert a
Float where
79 toFloating x
= F
# (divideFloat
# num denom
) where
80 !(F
# num
) = toFloating
. numerator . toRational $ x
81 !(F
# denom
) = toFloating
. denominator . toRational $ x
83 instance FloatConvert a a
where