doc: Minor haddock markup fix.
[altfloat.git] / Data / Floating / Classes.hs
blobf9611cdc8a4e091b2fe4b6acbd81ac369e93b8b0
1 {-
2 - Copyright (C) 2009 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.
7 -}
9 -- | Generic classes for floating point types. The interface is loosely based
10 -- off of the C math library.
11 module Data.Floating.Classes where
13 import Prelude hiding (Floating(..), RealFloat(..), RealFrac(..), Ord(..))
14 import Data.Ratio
15 import Data.Poset
17 -- | Classification of floating point values.
18 data FPClassification = FPInfinite | FPNaN | FPNormal | FPSubNormal | FPZero
19 deriving (Show, Read, Eq, Enum, Bounded)
21 -- | Class for types which can be rounded to integers.
23 -- Minimal complete definition: 'toIntegral' and 'round'.
24 class (Fractional a, Poset a) => Roundable a where
25 -- | Discards the fractional component from a value. Results in 'Nothing'
26 -- if the result cannot be represented as an integer, such as if the input
27 -- is infinite or NaN.
28 toIntegral :: Integral b => a -> Maybe b
29 ceiling :: a -> a
30 floor :: a -> a
31 truncate :: a -> a
32 round :: a -> a
34 floor x
35 | round x == x = x
36 | otherwise = round $ x - fromRational (1%2)
37 ceiling x
38 | round x == x = x
39 | otherwise = round $ x + fromRational (1%2)
40 truncate x
41 | x < 0 = ceiling x
42 | x > 0 = floor x
43 | otherwise = x
45 -- | Class for floating point types (real or complex-valued).
47 -- Minimal complete definition: everything.
48 class Fractional a => Floating a where
49 (**) :: a -> a -> a
50 sqrt :: a -> a
51 acos :: a -> a
52 asin :: a -> a
53 atan :: a -> a
54 cos :: a -> a
55 sin :: a -> a
56 tan :: a -> a
57 acosh :: a -> a
58 asinh :: a -> a
59 atanh :: a -> a
60 cosh :: a -> a
61 sinh :: a -> a
62 tanh :: a -> a
63 exp :: a -> a
64 log :: a -> a
66 -- | Class for real-valued floating point types.
68 -- Minimal complete definition: all except 'pi', 'infinity' and 'nan'.
69 class Floating a => RealFloat a where
70 fma :: a -> a -> a -> a
71 copysign :: a -> a -> a
72 nextafter :: a -> a -> a
73 atan2 :: a -> a -> a
74 fmod :: a -> a -> a
75 frem :: a -> a -> a
76 fquotRem :: a -> a -> (Int, a)
77 hypot :: a -> a -> a
78 cbrt :: a -> a
79 exp2 :: a -> a
80 expm1 :: a -> a
81 log10 :: a -> a
82 log1p :: a -> a
83 log2 :: a -> a
84 logb :: a -> a
85 erf :: a -> a
86 erfc :: a -> a
87 lgamma :: a -> a
88 tgamma :: a -> a
89 classify :: a -> FPClassification
90 infinity :: a
91 nan :: a
92 pi :: a
94 infinity = 1/0
95 nan = 0/0
96 pi = 4 * atan 1