/
Number.purs
363 lines (325 loc) · 7.36 KB
/
Number.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
-- | Functions for working with PureScripts builtin `Number` type.
module Data.Number
( fromString
, nan
, isNaN
, infinity
, isFinite
, abs
, acos
, asin
, atan
, atan2
, ceil
, cos
, exp
, floor
, log
, max
, min
, pow
, remainder, (%)
, round
, sign
, sin
, sqrt
, tan
, trunc
, e
, ln2
, ln10
, log10e
, log2e
, pi
, sqrt1_2
, sqrt2
, tau
) where
import Data.Function.Uncurried (Fn4, runFn4)
import Data.Maybe (Maybe(..))
-- | Not a number (NaN).
-- | ```purs
-- | > nan
-- | NaN
-- | ```
foreign import nan :: Number
-- | Test whether a number is NaN.
-- | ```purs
-- | > isNaN 0.0
-- | false
-- |
-- | > isNaN nan
-- | true
-- | ```
foreign import isNaN :: Number -> Boolean
-- | Positive infinity. For negative infinity use `(-infinity)`
-- | ```purs
-- | > infinity
-- | Infinity
-- |
-- | > (-infinity)
-- | - Infinity
-- | ```
foreign import infinity :: Number
-- | Test whether a number is finite.
-- | ```purs
-- | > isFinite 0.0
-- | true
-- |
-- | > isFinite infinity
-- | false
-- |
-- | > isFinite (-infinity)
-- | false
-- |
-- | > isFinite nan
-- | false
-- | ```
foreign import isFinite :: Number -> Boolean
-- | Attempt to parse a `Number` using JavaScripts `parseFloat`. Returns
-- | `Nothing` if the parse fails or if the result is not a finite number.
-- |
-- | Example:
-- | ```purs
-- | > fromString "123"
-- | (Just 123.0)
-- |
-- | > fromString "12.34"
-- | (Just 12.34)
-- |
-- | > fromString "1e4"
-- | (Just 10000.0)
-- |
-- | > fromString "1.2e4"
-- | (Just 12000.0)
-- |
-- | > fromString "bad"
-- | Nothing
-- | ```
-- |
-- | Note that `parseFloat` allows for trailing non-digit characters and
-- | whitespace as a prefix:
-- | ```
-- | > fromString " 1.2 ??"
-- | (Just 1.2)
-- | ```
fromString :: String -> Maybe Number
fromString str = runFn4 fromStringImpl str isFinite Just Nothing
foreign import fromStringImpl :: Fn4 String (Number -> Boolean) (forall a. a -> Maybe a) (forall a. Maybe a) (Maybe Number)
-- | Returns the absolute value of the argument.
-- | ```purs
-- | > x = -42.0
-- | > sign x * abs x == x
-- | true
-- | ```
foreign import abs :: Number -> Number
-- | Returns the inverse cosine in radians of the argument.
-- | ```purs
-- | > acos 0.0 == pi / 2.0
-- | true
-- | ```
foreign import acos :: Number -> Number
-- | Returns the inverse sine in radians of the argument.
-- | ```purs
-- | > asin 1.0 == pi / 2.0
-- | true
-- | ```
foreign import asin :: Number -> Number
-- | Returns the inverse tangent in radians of the argument.
-- | ```purs
-- | > atan 1.0 == pi / 4.0
-- | true
-- | ```
foreign import atan :: Number -> Number
-- | Four-quadrant tangent inverse. Given the arguments `y` and `x`, returns
-- | the inverse tangent of `y / x`, where the signs of both arguments are used
-- | to determine the sign of the result.
-- | If the first argument is negative, the result will be negative.
-- | The result is the angle between the positive x axis and a point `(x, y)`.
-- | ```purs
-- | > atan2 0.0 1.0
-- | 0.0
-- | > atan2 1.0 0.0 == pi / 2.0
-- | true
-- | ```
foreign import atan2 :: Number -> Number -> Number
-- | Returns the smallest integer not smaller than the argument.
-- | ```purs
-- | > ceil 1.5
-- | 2.0
-- | ```
foreign import ceil :: Number -> Number
-- | Returns the cosine of the argument, where the argument is in radians.
-- | ```purs
-- | > cos (pi / 4.0) == sqrt2 / 2.0
-- | true
-- | ```
foreign import cos :: Number -> Number
-- | Returns `e` exponentiated to the power of the argument.
-- | ```purs
-- | > exp 1.0
-- | 2.718281828459045
-- | ```
foreign import exp :: Number -> Number
-- | Returns the largest integer not larger than the argument.
-- | ```purs
-- | > floor 1.5
-- | 1.0
-- | ```
foreign import floor :: Number -> Number
-- | Returns the natural logarithm of a number.
-- | ```purs
-- | > log e
-- | 1.0
foreign import log :: Number -> Number
-- | Returns the largest of two numbers. Unlike `max` in Data.Ord this version
-- | returns NaN if either argument is NaN.
foreign import max :: Number -> Number -> Number
-- | Returns the smallest of two numbers. Unlike `min` in Data.Ord this version
-- | returns NaN if either argument is NaN.
foreign import min :: Number -> Number -> Number
-- | Return the first argument exponentiated to the power of the second argument.
-- | ```purs
-- | > pow 3.0 2.0
-- | 9.0
-- | > sqrt 42.0 == pow 42.0 0.5
-- | true
-- | ```
foreign import pow :: Number -> Number -> Number
-- | Computes the remainder after division. This is the same as JavaScript's `%` operator.
-- ```purs
-- > 5.3 % 2.0
-- 1.2999999999999998
-- ```
foreign import remainder :: Number -> Number -> Number
infixl 7 remainder as %
-- | Returns the integer closest to the argument.
-- | ```purs
-- | > round 1.5
-- | 2.0
-- | ```
foreign import round :: Number -> Number
-- | Returns either a positive or negative +/- 1, indicating the sign of the
-- | argument. If the argument is 0, it will return a +/- 0. If the argument is
-- | NaN it will return NaN.
-- | ```purs
-- | > x = -42.0
-- | > sign x * abs x == x
-- | true
-- | ```
foreign import sign :: Number -> Number
-- | Returns the sine of the argument, where the argument is in radians.
-- | ```purs
-- | > sin (pi / 2.0)
-- | 1.0
-- | ```
foreign import sin :: Number -> Number
-- | Returns the square root of the argument.
-- | ```purs
-- | > sqrt 49.0
-- | 7.0
-- | ```
foreign import sqrt :: Number -> Number
-- | Returns the tangent of the argument, where the argument is in radians.
-- | ```
-- | > tan (pi / 4.0)
-- | 0.9999999999999999
-- | ```
foreign import tan :: Number -> Number
-- | Truncates the decimal portion of a number. Equivalent to `floor` if the
-- | number is positive, and `ceil` if the number is negative.
-- | ```purs
-- | ceil 1.5
-- | 2.0
-- | ```
foreign import trunc :: Number -> Number
-- | The base of the natural logarithm, also known as Euler's number or *e*.
-- | ```purs
-- | > log e
-- | 1.0
-- |
-- | > exp 1.0 == e
-- | true
-- |
-- | > e
-- | 2.718281828459045
-- | ```
e :: Number
e = 2.718281828459045
-- | The natural logarithm of 2.
-- | ```purs
-- | > log 2.0 == ln2
-- | true
-- |
-- | > ln2
-- | 0.6931471805599453
-- | ```
ln2 :: Number
ln2 = 0.6931471805599453
-- | The natural logarithm of 10.
-- | ```purs
-- | > log 10.0 == ln10
-- | true
-- |
-- | > ln10
-- | 2.302585092994046
-- | ```
ln10 :: Number
ln10 = 2.302585092994046
-- | Base 10 logarithm of `e`.
-- | ```purs
-- | > 1.0 / ln10 - log10e
-- | -5.551115123125783e-17
-- |
-- | > log10e
-- | 0.4342944819032518
-- | ```
log10e :: Number
log10e = 0.4342944819032518
-- | The base 2 logarithm of `e`.
-- | ```purs
-- | > 1.0 / ln2 == log2e
-- | true
-- |
-- | > log2e
-- | 1.4426950408889634
-- | ```
log2e :: Number
log2e = 1.4426950408889634
-- | The ratio of the circumference of a circle to its diameter.
-- | ```purs
-- | > pi
-- | 3.141592653589793
-- | ```
pi :: Number
pi = 3.141592653589793
-- | The square root of one half.
-- | ```purs
-- | > sqrt 0.5 == sqrt1_2
-- | true
-- |
-- | > sqrt1_2
-- | 0.7071067811865476
-- | ```
sqrt1_2 :: Number
sqrt1_2 = 0.7071067811865476
-- | The square root of two.
-- | ```purs
-- | > sqrt 2.0 == sqrt2
-- | true
-- |
-- | > sqrt2
-- | 1.4142135623730951
-- | ```
sqrt2 :: Number
sqrt2 = 1.4142135623730951
-- | The ratio of the circumference of a circle to its radius.
-- | ```purs
-- | > 2.0 * pi == tau
-- | true
-- |
-- | > tau
-- | 6.283185307179586
-- | ```
tau :: Number
tau = 6.283185307179586