-
Notifications
You must be signed in to change notification settings - Fork 6
/
Float32.purs
63 lines (55 loc) · 1.68 KB
/
Float32.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module Data.Float32
( Float32
, fromNumber
, fromNumber'
, toNumber
) where
import Prelude
( class Eq
, class Ord
, class Show
, class Semiring
, class Ring
, class Bounded
, class EuclideanRing
, class CommutativeRing
, class DivisionRing
, top
, bottom
, (==)
, (||)
)
import Data.Maybe (Maybe(..))
-- | [32-bit single-precision floating-point](https://en.wikipedia.org/wiki/Single-precision_floating-point_format)
-- | number.
newtype Float32 = Float32 Number
derive newtype instance eqFloat32 :: Eq Float32
derive newtype instance ordFloat32 :: Ord Float32
derive newtype instance semiringFloat32 :: Semiring Float32
derive newtype instance ringFloat32 :: Ring Float32
derive newtype instance commutativeringFloat32 :: CommutativeRing Float32
derive newtype instance euclideanringFloat32 :: EuclideanRing Float32
derive newtype instance divisionringFloat32 :: DivisionRing Float32
derive newtype instance showFloat32 :: Show Float32
instance boundedFloat32 :: Bounded Float32 where
top = float32Top
bottom = float32Bottom
foreign import float32Top :: Float32
foreign import float32Bottom :: Float32
-- | Uses `Math.fround()` to convert to a `Float32`.
-- | Returns `Nothing` when outside the range.
fromNumber :: Number -> Maybe Float32
fromNumber x =
let
r = fromNumberImpl x
in
if r == top || r == bottom then Nothing else Just (Float32 r)
-- | Uses `Math.fround()` to convert to a `Float32`.
-- | Returns *0.0* when outside the range.
fromNumber' :: Number -> Float32
fromNumber' x = case fromNumber x of
Nothing -> Float32 0.0
Just y -> y
foreign import fromNumberImpl :: Number -> Number
toNumber :: Float32 -> Number
toNumber (Float32 x) = x