@@ -5,7 +5,7 @@ module Data.Int
5
5
, round
6
6
, toNumber
7
7
, fromString
8
- , Radix ()
8
+ , Radix
9
9
, radix
10
10
, binary
11
11
, octal
@@ -19,19 +19,14 @@ module Data.Int
19
19
, pow
20
20
) where
21
21
22
- import Data.Boolean (otherwise )
23
- import Data.BooleanAlgebra ((&&))
24
- import Data.Bounded (top , bottom )
25
- import Data.Eq ((==), (/=))
26
- import Data.Function ((<<<))
22
+ import Prelude
23
+
27
24
import Data.Int.Bits ((.&.))
28
- import Data.Maybe (Maybe (..), fromJust )
29
- import Data.Ord ((<=), (>=) )
25
+ import Data.Maybe (Maybe (..), fromMaybe )
26
+ import Global ( infinity )
30
27
31
28
import Math as Math
32
29
33
- import Partial.Unsafe (unsafePartial )
34
-
35
30
-- | Creates an `Int` from a `Number` value. The number must already be an
36
31
-- | integer and fall within the valid range of values for the `Int` type
37
32
-- | otherwise `Nothing` is returned.
@@ -45,28 +40,32 @@ foreign import fromNumberImpl
45
40
-> Maybe Int
46
41
47
42
-- | Convert a `Number` to an `Int`, by taking the closest integer equal to or
48
- -- | less than the argument. Values outside the `Int` range are clamped.
43
+ -- | less than the argument. Values outside the `Int` range are clamped, `NaN`
44
+ -- | and `Infinity` values return 0.
49
45
floor :: Number -> Int
50
46
floor = unsafeClamp <<< Math .floor
51
47
52
48
-- | Convert a `Number` to an `Int`, by taking the closest integer equal to or
53
- -- | greater than the argument. Values outside the `Int` range are clamped.
49
+ -- | greater than the argument. Values outside the `Int` range are clamped,
50
+ -- | `NaN` and `Infinity` values return 0.
54
51
ceil :: Number -> Int
55
52
ceil = unsafeClamp <<< Math .ceil
56
53
57
54
-- | Convert a `Number` to an `Int`, by taking the nearest integer to the
58
- -- | argument. Values outside the `Int` range are clamped.
55
+ -- | argument. Values outside the `Int` range are clamped, `NaN` and `Infinity`
56
+ -- | values return 0.
59
57
round :: Number -> Int
60
58
round = unsafeClamp <<< Math .round
61
59
62
60
-- | Convert an integral `Number` to an `Int`, by clamping to the `Int` range.
63
- -- | This function will throw an error at runtime if the argument is
64
- -- | non-integral.
61
+ -- | This function will return 0 if the input is `NaN` or an `Infinity`.
65
62
unsafeClamp :: Number -> Int
66
63
unsafeClamp x
64
+ | x == infinity = 0
65
+ | x == -infinity = 0
67
66
| x >= toNumber top = top
68
67
| x <= toNumber bottom = bottom
69
- | otherwise = unsafePartial (fromJust (fromNumber x) )
68
+ | otherwise = fromMaybe 0 (fromNumber x)
70
69
71
70
-- | Converts an `Int` value back into a `Number`. Any `Int` is a valid `Number`
72
71
-- | so there is no loss of precision with this function.
0 commit comments