ic-mops 0.8.4 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/.mops/base@0.7.4/LICENSE +208 -0
  2. package/.mops/base@0.7.4/README.md +64 -0
  3. package/.mops/base@0.7.4/mops.toml +5 -0
  4. package/.mops/base@0.7.4/src/Array.mo +686 -0
  5. package/.mops/base@0.7.4/src/AssocList.mo +203 -0
  6. package/.mops/base@0.7.4/src/Blob.mo +55 -0
  7. package/.mops/base@0.7.4/src/Bool.mo +44 -0
  8. package/.mops/base@0.7.4/src/Buffer.mo +1937 -0
  9. package/.mops/base@0.7.4/src/CertifiedData.mo +29 -0
  10. package/.mops/base@0.7.4/src/Char.mo +67 -0
  11. package/.mops/base@0.7.4/src/Debug.mo +15 -0
  12. package/.mops/base@0.7.4/src/Deque.mo +75 -0
  13. package/.mops/base@0.7.4/src/Error.mo +41 -0
  14. package/.mops/base@0.7.4/src/ExperimentalCycles.mo +51 -0
  15. package/.mops/base@0.7.4/src/ExperimentalInternetComputer.mo +36 -0
  16. package/.mops/base@0.7.4/src/ExperimentalStableMemory.mo +121 -0
  17. package/.mops/base@0.7.4/src/Float.mo +150 -0
  18. package/.mops/base@0.7.4/src/Func.mo +38 -0
  19. package/.mops/base@0.7.4/src/Hash.mo +83 -0
  20. package/.mops/base@0.7.4/src/HashMap.mo +229 -0
  21. package/.mops/base@0.7.4/src/Heap.mo +113 -0
  22. package/.mops/base@0.7.4/src/Int.mo +150 -0
  23. package/.mops/base@0.7.4/src/Int16.mo +159 -0
  24. package/.mops/base@0.7.4/src/Int32.mo +160 -0
  25. package/.mops/base@0.7.4/src/Int64.mo +161 -0
  26. package/.mops/base@0.7.4/src/Int8.mo +160 -0
  27. package/.mops/base@0.7.4/src/Iter.mo +220 -0
  28. package/.mops/base@0.7.4/src/IterType.mo +7 -0
  29. package/.mops/base@0.7.4/src/List.mo +433 -0
  30. package/.mops/base@0.7.4/src/Nat.mo +75 -0
  31. package/.mops/base@0.7.4/src/Nat16.mo +146 -0
  32. package/.mops/base@0.7.4/src/Nat32.mo +146 -0
  33. package/.mops/base@0.7.4/src/Nat64.mo +146 -0
  34. package/.mops/base@0.7.4/src/Nat8.mo +146 -0
  35. package/.mops/base@0.7.4/src/None.mo +19 -0
  36. package/.mops/base@0.7.4/src/Option.mo +160 -0
  37. package/.mops/base@0.7.4/src/Order.mo +46 -0
  38. package/.mops/base@0.7.4/src/Prelude.mo +33 -0
  39. package/.mops/base@0.7.4/src/Principal.mo +58 -0
  40. package/.mops/base@0.7.4/src/RBTree.mo +218 -0
  41. package/.mops/base@0.7.4/src/Random.mo +188 -0
  42. package/.mops/base@0.7.4/src/Result.mo +210 -0
  43. package/.mops/base@0.7.4/src/Stack.mo +40 -0
  44. package/.mops/base@0.7.4/src/Text.mo +615 -0
  45. package/.mops/base@0.7.4/src/Time.mo +37 -0
  46. package/.mops/base@0.7.4/src/Trie.mo +1200 -0
  47. package/.mops/base@0.7.4/src/TrieMap.mo +180 -0
  48. package/.mops/base@0.7.4/src/TrieSet.mo +97 -0
  49. package/.mops/base@0.8.3/LICENSE +208 -0
  50. package/.mops/base@0.8.3/README.md +64 -0
  51. package/.mops/base@0.8.3/mops.toml +6 -0
  52. package/.mops/base@0.8.3/src/Array.mo +717 -0
  53. package/.mops/base@0.8.3/src/AssocList.mo +404 -0
  54. package/.mops/base@0.8.3/src/Blob.mo +212 -0
  55. package/.mops/base@0.8.3/src/Bool.mo +44 -0
  56. package/.mops/base@0.8.3/src/Buffer.mo +2660 -0
  57. package/.mops/base@0.8.3/src/CertifiedData.mo +53 -0
  58. package/.mops/base@0.8.3/src/Char.mo +65 -0
  59. package/.mops/base@0.8.3/src/Debug.mo +56 -0
  60. package/.mops/base@0.8.3/src/Deque.mo +243 -0
  61. package/.mops/base@0.8.3/src/Error.mo +68 -0
  62. package/.mops/base@0.8.3/src/ExperimentalCycles.mo +151 -0
  63. package/.mops/base@0.8.3/src/ExperimentalInternetComputer.mo +60 -0
  64. package/.mops/base@0.8.3/src/ExperimentalStableMemory.mo +348 -0
  65. package/.mops/base@0.8.3/src/Float.mo +843 -0
  66. package/.mops/base@0.8.3/src/Func.mo +46 -0
  67. package/.mops/base@0.8.3/src/Hash.mo +82 -0
  68. package/.mops/base@0.8.3/src/HashMap.mo +457 -0
  69. package/.mops/base@0.8.3/src/Heap.mo +233 -0
  70. package/.mops/base@0.8.3/src/Int.mo +365 -0
  71. package/.mops/base@0.8.3/src/Int16.mo +521 -0
  72. package/.mops/base@0.8.3/src/Int32.mo +522 -0
  73. package/.mops/base@0.8.3/src/Int64.mo +522 -0
  74. package/.mops/base@0.8.3/src/Int8.mo +522 -0
  75. package/.mops/base@0.8.3/src/Iter.mo +227 -0
  76. package/.mops/base@0.8.3/src/IterType.mo +7 -0
  77. package/.mops/base@0.8.3/src/List.mo +930 -0
  78. package/.mops/base@0.8.3/src/Nat.mo +305 -0
  79. package/.mops/base@0.8.3/src/Nat16.mo +144 -0
  80. package/.mops/base@0.8.3/src/Nat32.mo +144 -0
  81. package/.mops/base@0.8.3/src/Nat64.mo +144 -0
  82. package/.mops/base@0.8.3/src/Nat8.mo +144 -0
  83. package/.mops/base@0.8.3/src/None.mo +19 -0
  84. package/.mops/base@0.8.3/src/Option.mo +154 -0
  85. package/.mops/base@0.8.3/src/Order.mo +46 -0
  86. package/.mops/base@0.8.3/src/Prelude.mo +33 -0
  87. package/.mops/base@0.8.3/src/Principal.mo +249 -0
  88. package/.mops/base@0.8.3/src/RBTree.mo +681 -0
  89. package/.mops/base@0.8.3/src/Random.mo +270 -0
  90. package/.mops/base@0.8.3/src/Result.mo +209 -0
  91. package/.mops/base@0.8.3/src/Stack.mo +93 -0
  92. package/.mops/base@0.8.3/src/Text.mo +761 -0
  93. package/.mops/base@0.8.3/src/Time.mo +36 -0
  94. package/.mops/base@0.8.3/src/Timer.mo +62 -0
  95. package/.mops/base@0.8.3/src/Trie.mo +1603 -0
  96. package/.mops/base@0.8.3/src/TrieMap.mo +392 -0
  97. package/.mops/base@0.8.3/src/TrieSet.mo +148 -0
  98. package/network.txt +1 -0
  99. package/package.json +2 -2
@@ -0,0 +1,843 @@
1
+ /// Double precision (64-bit) floating-point numbers in IEEE 754 representation.
2
+ ///
3
+ /// This module contains common floating-point constants and utility functions.
4
+ ///
5
+ /// Notation for special values in the documentation below:
6
+ /// `+inf`: Positive infinity
7
+ /// `-inf`: Negative infinity
8
+ /// `NaN`: "not a number" (can have different sign bit values, but `NaN != NaN` regardless of the sign).
9
+ ///
10
+ /// Note:
11
+ /// Floating point numbers have limited precision and operations may inherently result in numerical errors.
12
+ ///
13
+ /// Examples of numerical errors:
14
+ /// ```motoko
15
+ /// 0.1 + 0.1 + 0.1 == 0.3 // => false
16
+ /// ```
17
+ ///
18
+ /// ```motoko
19
+ /// 1e16 + 1.0 != 1e16 // => false
20
+ /// ```
21
+ ///
22
+ /// (and many more cases)
23
+ ///
24
+ /// Advice:
25
+ /// * Floating point number comparisons by `==` or `!=` are discouraged. Instead, it is better to compare
26
+ /// floating-point numbers with a numerical tolerance, called epsilon.
27
+ ///
28
+ /// Example:
29
+ /// ```motoko
30
+ /// import Float "mo:base/Float";
31
+ /// let x = 0.1 + 0.1 + 0.1;
32
+ /// let y = 0.3;
33
+ ///
34
+ /// let epsilon = 1e-6; // This depends on the application case (needs a numerical error analysis).
35
+ /// Float.equalWithin(x, y, epsilon) // => true
36
+ /// ```
37
+ ///
38
+ /// * For absolute precision, it is recommened to encode the fraction number as a pair of a Nat for the base
39
+ /// and a Nat for the exponent (decimal point).
40
+ ///
41
+
42
+ import Prim "mo:⛔";
43
+ import Int "Int";
44
+
45
+ module {
46
+
47
+ /// 64-bit floating point number type.
48
+ public type Float = Prim.Types.Float;
49
+
50
+ /// Ratio of the circumference of a circle to its diameter.
51
+ /// Note: Limited precision.
52
+ public let pi : Float = 3.14159265358979323846; // taken from musl math.h
53
+
54
+ /// Base of the natural logarithm.
55
+ /// Note: Limited precision.
56
+ public let e : Float = 2.7182818284590452354; // taken from musl math.h
57
+
58
+ /// Determines whether the `number` is a `NaN` ("not a number" in the floating point representation).
59
+ /// Notes:
60
+ /// * Equality test of `NaN` with itself or another number is always `false`.
61
+ /// * There exist many internal `NaN` value representations, such as positive and negative NaN,
62
+ /// signalling and quiet nans, each with many different bit representations.
63
+ ///
64
+ /// Example:
65
+ /// ```motoko
66
+ /// import Float "mo:base/Float";
67
+ ///
68
+ /// Float.isNaN(0.0/0.0) // => true
69
+ /// ```
70
+ public func isNaN(number : Float) : Bool {
71
+ number != number
72
+ };
73
+
74
+ /// Returns the absolute value of `x`.
75
+ ///
76
+ /// Special cases:
77
+ /// ```
78
+ /// abs(+inf) => +inf
79
+ /// abs(-inf) => +inf
80
+ /// abs(NaN) => NaN
81
+ /// abs(-0.0) => 0.0
82
+ /// ```
83
+ ///
84
+ /// Example:
85
+ /// ```motoko
86
+ /// import Float "mo:base/Float";
87
+ ///
88
+ /// Float.abs(-1.2) // => 1.2
89
+ /// ```
90
+ public let abs : (x : Float) -> Float = Prim.floatAbs;
91
+
92
+ /// Returns the square root of `x`.
93
+ ///
94
+ /// Special cases:
95
+ /// ```
96
+ /// sqrt(+inf) => +inf
97
+ /// sqrt(-0.0) => -0.0
98
+ /// sqrt(x) => NaN if x < 0.0
99
+ /// sqrt(NaN) => NaN
100
+ /// ```
101
+ ///
102
+ /// Example:
103
+ /// ```motoko
104
+ /// import Float "mo:base/Float";
105
+ ///
106
+ /// Float.sqrt(6.25) // => 2.5
107
+ /// ```
108
+ public let sqrt : (x : Float) -> Float = Prim.floatSqrt;
109
+
110
+ /// Returns the smallest integral float greater than or equal to `x`.
111
+ ///
112
+ /// Special cases:
113
+ /// ```
114
+ /// ceil(+inf) => +inf
115
+ /// ceil(-inf) => -inf
116
+ /// ceil(NaN) => NaN
117
+ /// ceil(0.0) => 0.0
118
+ /// ceil(-0.0) => -0.0
119
+ /// ```
120
+ ///
121
+ /// Example:
122
+ /// ```motoko
123
+ /// import Float "mo:base/Float";
124
+ ///
125
+ /// Float.ceil(1.2) // => 2.0
126
+ /// ```
127
+ public let ceil : (x : Float) -> Float = Prim.floatCeil;
128
+
129
+ /// Returns the largest integral float less than or equal to `x`.
130
+ ///
131
+ /// Special cases:
132
+ /// ```
133
+ /// floor(+inf) => +inf
134
+ /// floor(-inf) => -inf
135
+ /// floor(NaN) => NaN
136
+ /// floor(0.0) => 0.0
137
+ /// floor(-0.0) => -0.0
138
+ /// ```
139
+ ///
140
+ /// Example:
141
+ /// ```motoko
142
+ /// import Float "mo:base/Float";
143
+ ///
144
+ /// Float.floor(1.2) // => 1.0
145
+ /// ```
146
+ public let floor : (x : Float) -> Float = Prim.floatFloor;
147
+
148
+ /// Returns the nearest integral float not greater in magnitude than `x`.
149
+ /// This is equilvent to returning `x` with truncating its decimal places.
150
+ ///
151
+ /// Special cases:
152
+ /// ```
153
+ /// trunc(+inf) => +inf
154
+ /// trunc(-inf) => -inf
155
+ /// trunc(NaN) => NaN
156
+ /// trunc(0.0) => 0.0
157
+ /// trunc(-0.0) => -0.0
158
+ /// ```
159
+ ///
160
+ /// Example:
161
+ /// ```motoko
162
+ /// import Float "mo:base/Float";
163
+ ///
164
+ /// Float.trunc(2.75) // => 2.0
165
+ /// ```
166
+ public let trunc : (x : Float) -> Float = Prim.floatTrunc;
167
+
168
+ /// Returns the nearest integral float to `x`.
169
+ /// A decimal place of exactly .5 is rounded up for `x > 0`
170
+ /// and rounded down for `x < 0`
171
+ ///
172
+ /// Special cases:
173
+ /// ```
174
+ /// nearest(+inf) => +inf
175
+ /// nearest(-inf) => -inf
176
+ /// nearest(NaN) => NaN
177
+ /// nearest(0.0) => 0.0
178
+ /// nearest(-0.0) => -0.0
179
+ /// ```
180
+ ///
181
+ /// Example:
182
+ /// ```motoko
183
+ /// import Float "mo:base/Float";
184
+ ///
185
+ /// Float.nearest(2.75) // => 3.0
186
+ /// ```
187
+ public let nearest : (x : Float) -> Float = Prim.floatNearest;
188
+
189
+ /// Returns `x` if `x` and `y` have same sign, otherwise `x` with negated sign.
190
+ ///
191
+ /// The sign bit of zero, infinity, and `NaN` is considered.
192
+ ///
193
+ /// Example:
194
+ /// ```motoko
195
+ /// import Float "mo:base/Float";
196
+ ///
197
+ /// Float.copySign(1.2, -2.3) // => -1.2
198
+ /// ```
199
+ public let copySign : (x : Float, y : Float) -> Float = Prim.floatCopySign;
200
+
201
+ /// Returns the smaller value of `x` and `y`.
202
+ ///
203
+ /// Special cases:
204
+ /// ```
205
+ /// min(NaN, y) => NaN for any Float y
206
+ /// min(x, NaN) => NaN for any Float x
207
+ /// ```
208
+ ///
209
+ /// Example:
210
+ /// ```motoko
211
+ /// import Float "mo:base/Float";
212
+ ///
213
+ /// Float.min(1.2, -2.3) // => -2.3 (with numerical imprecision)
214
+ /// ```
215
+ public let min : (x : Float, y : Float) -> Float = Prim.floatMin;
216
+
217
+ /// Returns the larger value of `x` and `y`.
218
+ ///
219
+ /// Special cases:
220
+ /// ```
221
+ /// max(NaN, y) => NaN for any Float y
222
+ /// max(x, NaN) => NaN for any Float x
223
+ /// ```
224
+ ///
225
+ /// Example:
226
+ /// ```motoko
227
+ /// import Float "mo:base/Float";
228
+ ///
229
+ /// Float.max(1.2, -2.3) // => 1.2
230
+ /// ```
231
+ public let max : (x : Float, y : Float) -> Float = Prim.floatMax;
232
+
233
+ /// Returns the sine of the radian angle `x`.
234
+ ///
235
+ /// Special cases:
236
+ /// ```
237
+ /// sin(+inf) => NaN
238
+ /// sin(-inf) => NaN
239
+ /// sin(NaN) => NaN
240
+ /// ```
241
+ ///
242
+ /// Example:
243
+ /// ```motoko
244
+ /// import Float "mo:base/Float";
245
+ ///
246
+ /// Float.sin(Float.pi / 2) // => 1.0
247
+ /// ```
248
+ public let sin : (x : Float) -> Float = Prim.sin;
249
+
250
+ /// Returns the cosine of the radian angle `x`.
251
+ ///
252
+ /// Special cases:
253
+ /// ```
254
+ /// cos(+inf) => NaN
255
+ /// cos(-inf) => NaN
256
+ /// cos(NaN) => NaN
257
+ /// ```
258
+ ///
259
+ /// Example:
260
+ /// ```motoko
261
+ /// import Float "mo:base/Float";
262
+ ///
263
+ /// Float.cos(Float.pi / 2) // => 0.0 (with numerical imprecision)
264
+ /// ```
265
+ public let cos : (x : Float) -> Float = Prim.cos;
266
+
267
+ /// Returns the tangent of the radian angle `x`.
268
+ ///
269
+ /// Special cases:
270
+ /// ```
271
+ /// tan(+inf) => NaN
272
+ /// tan(-inf) => NaN
273
+ /// tan(NaN) => NaN
274
+ /// ```
275
+ ///
276
+ /// Example:
277
+ /// ```motoko
278
+ /// import Float "mo:base/Float";
279
+ ///
280
+ /// Float.tan(Float.pi / 4) // => 1.0 (with numerical imprecision)
281
+ /// ```
282
+ public let tan : (x : Float) -> Float = Prim.tan;
283
+
284
+ /// Returns the arc sine of `x` in radians.
285
+ ///
286
+ /// Special cases:
287
+ /// ```
288
+ /// arcsin(x) => NaN if x > 1.0
289
+ /// arcsin(x) => NaN if x < -1.0
290
+ /// arcsin(NaN) => NaN
291
+ /// ```
292
+ ///
293
+ /// Example:
294
+ /// ```motoko
295
+ /// import Float "mo:base/Float";
296
+ ///
297
+ /// Float.arcsin(1.0) // => Float.pi / 2
298
+ /// ```
299
+ public let arcsin : (x : Float) -> Float = Prim.arcsin;
300
+
301
+ /// Returns the arc cosine of `x` in radians.
302
+ ///
303
+ /// Special cases:
304
+ /// ```
305
+ /// arccos(x) => NaN if x > 1.0
306
+ /// arccos(x) => NaN if x < -1.0
307
+ /// arcos(NaN) => NaN
308
+ /// ```
309
+ ///
310
+ /// Example:
311
+ /// ```motoko
312
+ /// import Float "mo:base/Float";
313
+ ///
314
+ /// Float.arccos(1.0) // => 0.0
315
+ /// ```
316
+ public let arccos : (x : Float) -> Float = Prim.arccos;
317
+
318
+ /// Returns the arc tangent of `x` in radians.
319
+ ///
320
+ /// Special cases:
321
+ /// ```
322
+ /// arctan(+inf) => pi / 2
323
+ /// arctan(-inf) => -pi / 2
324
+ /// arctan(NaN) => NaN
325
+ /// ```
326
+ ///
327
+ /// Example:
328
+ /// ```motoko
329
+ /// import Float "mo:base/Float";
330
+ ///
331
+ /// Float.arctan(1.0) // => Float.pi / 4
332
+ /// ```
333
+ public let arctan : (x : Float) -> Float = Prim.arctan;
334
+
335
+ /// Given `(y,x)`, returns the arc tangent in radians of `y/x` based on the signs of both values to determine the correct quadrant.
336
+ ///
337
+ /// Special cases:
338
+ /// ```
339
+ /// arctan2(0.0, 0.0) => 0.0
340
+ /// arctan2(-0.0, 0.0) => -0.0
341
+ /// arctan2(0.0, -0.0) => pi
342
+ /// arctan2(-0.0, -0.0) => -pi
343
+ /// arctan2(+inf, +inf) => pi / 4
344
+ /// arctan2(+inf, -inf) => 3 * pi / 4
345
+ /// arctan2(-inf, +inf) => -pi / 4
346
+ /// arctan2(-inf, -inf) => -3 * pi / 4
347
+ /// arctan2(NaN, x) => NaN for any Float x
348
+ /// arctan2(y, NaN) => NaN for any Float y
349
+ /// ```
350
+ ///
351
+ /// Example:
352
+ /// ```motoko
353
+ /// import Float "mo:base/Float";
354
+ ///
355
+ /// let sqrt2over2 = Float.sqrt(2) / 2;
356
+ /// Float.arctan2(sqrt2over2, sqrt2over2) // => Float.pi / 4
357
+ /// ```
358
+ public let arctan2 : (y : Float, x : Float) -> Float = Prim.arctan2;
359
+
360
+ /// Returns the value of `e` raised to the `x`-th power.
361
+ ///
362
+ /// Special cases:
363
+ /// ```
364
+ /// exp(+inf) => +inf
365
+ /// exp(-inf) => 0.0
366
+ /// exp(NaN) => NaN
367
+ /// ```
368
+ ///
369
+ /// Example:
370
+ /// ```motoko
371
+ /// import Float "mo:base/Float";
372
+ ///
373
+ /// Float.exp(1.0) // => Float.e
374
+ /// ```
375
+ public let exp : (x : Float) -> Float = Prim.exp;
376
+
377
+ /// Returns the natural logarithm (base-`e`) of `x`.
378
+ ///
379
+ /// Special cases:
380
+ /// ```
381
+ /// log(0.0) => -inf
382
+ /// log(-0.0) => -inf
383
+ /// log(x) => NaN if x < 0.0
384
+ /// log(+inf) => +inf
385
+ /// log(NaN) => NaN
386
+ /// ```
387
+ ///
388
+ /// Example:
389
+ /// ```motoko
390
+ /// import Float "mo:base/Float";
391
+ ///
392
+ /// Float.log(Float.e) // => 1.0
393
+ /// ```
394
+ public let log : (x : Float) -> Float = Prim.log;
395
+
396
+ /// Formatting. `format(fmt, x)` formats `x` to `Text` according to the
397
+ /// formatting directive `fmt`, which can take one of the following forms:
398
+ ///
399
+ /// * `#fix prec` as fixed-point format with `prec` digits
400
+ /// * `#exp prec` as exponential format with `prec` digits
401
+ /// * `#gen prec` as generic format with `prec` digits
402
+ /// * `#hex prec` as hexadecimal format with `prec` digits
403
+ /// * `#exact` as exact format that can be decoded without loss.
404
+ ///
405
+ /// `-0.0` is formatted with negative sign bit.
406
+ /// Positive infinity is formatted as `inf`.
407
+ /// Negative infinity is formatted as `-inf`.
408
+ /// `NaN` is formatted as `NaN` or `-NaN` depending on its sign bit.
409
+ ///
410
+ /// Example:
411
+ /// ```motoko
412
+ /// import Float "mo:base/Float";
413
+ ///
414
+ /// Float.format(#exp 3, 123.0) // => "1.230e+02"
415
+ /// ```
416
+ public func format(fmt : { #fix : Nat8; #exp : Nat8; #gen : Nat8; #hex : Nat8; #exact }, x : Float) : Text = switch fmt {
417
+ case (#fix(prec)) { Prim.floatToFormattedText(x, prec, 0) };
418
+ case (#exp(prec)) { Prim.floatToFormattedText(x, prec, 1) };
419
+ case (#gen(prec)) { Prim.floatToFormattedText(x, prec, 2) };
420
+ case (#hex(prec)) { Prim.floatToFormattedText(x, prec, 3) };
421
+ case (#exact) { Prim.floatToFormattedText(x, 17, 2) }
422
+ };
423
+
424
+ /// Conversion to Text. Use `format(fmt, x)` for more detailed control.
425
+ ///
426
+ /// `-0.0` is formatted with negative sign bit.
427
+ /// Positive infinity is formatted as `inf`.
428
+ /// Negative infinity is formatted as `-inf`.
429
+ /// `NaN` is formatted as `NaN` or `-NaN` depending on its sign bit.
430
+ ///
431
+ /// Example:
432
+ /// ```motoko
433
+ /// import Float "mo:base/Float";
434
+ ///
435
+ /// Float.toText(0.12) // => "0.12"
436
+ /// ```
437
+ public let toText : Float -> Text = Prim.floatToText;
438
+
439
+ /// Conversion to Int64 by truncating Float, equivalent to `toInt64(trunc(f))`
440
+ ///
441
+ /// Traps if the floating point number is larger or smaller than the representable Int64.
442
+ /// Also traps for `inf`, `-inf`, and `NaN`.
443
+ ///
444
+ /// Example:
445
+ /// ```motoko
446
+ /// import Float "mo:base/Float";
447
+ ///
448
+ /// Float.toInt64(-12.3) // => -12
449
+ /// ```
450
+ public let toInt64 : Float -> Int64 = Prim.floatToInt64;
451
+
452
+ /// Conversion from Int64.
453
+ ///
454
+ /// Note: The floating point number may be imprecise for large or small Int64.
455
+ ///
456
+ /// Example:
457
+ /// ```motoko
458
+ /// import Float "mo:base/Float";
459
+ ///
460
+ /// Float.fromInt64(-42) // => -42.0
461
+ /// ```
462
+ public let fromInt64 : Int64 -> Float = Prim.int64ToFloat;
463
+
464
+ /// Conversion to Int.
465
+ ///
466
+ /// Traps for `inf`, `-inf`, and `NaN`.
467
+ ///
468
+ /// Example:
469
+ /// ```motoko
470
+ /// import Float "mo:base/Float";
471
+ ///
472
+ /// Float.toInt(1.2e6) // => +1_200_000
473
+ /// ```
474
+ public let toInt : Float -> Int = Prim.floatToInt;
475
+
476
+ /// Conversion from Int. May result in `Inf`.
477
+ ///
478
+ /// Note: The floating point number may be imprecise for large or small Int values.
479
+ /// Returns `inf` if the integer is greater than the maximum floating point number.
480
+ /// Returns `-inf` if the integer is less than the minimum floating point number.
481
+ ///
482
+ /// Example:
483
+ /// ```motoko
484
+ /// import Float "mo:base/Float";
485
+ ///
486
+ /// Float.fromInt(-123) // => -123.0
487
+ /// ```
488
+ public let fromInt : Int -> Float = Prim.intToFloat;
489
+
490
+ /// Returns `x == y`.
491
+ /// @deprecated Use `Float.equalWithin()` as this function does not consider numerical errors.
492
+ public func equal(x : Float, y : Float) : Bool { x == y };
493
+
494
+ /// Returns `x != y`.
495
+ /// @deprecated Use `Float.notEqualWithin()` as this function does not consider numerical errors.
496
+ public func notEqual(x : Float, y : Float) : Bool { x != y };
497
+
498
+ /// Determines whether `x` is equal to `y` within the defined tolerance of `epsilon`.
499
+ /// The `epsilon` considers numerical erros, see comment above.
500
+ /// Equivalent to `Float.abs(x - y) <= epsilon` for a non-negative epsilon.
501
+ ///
502
+ /// Traps if `epsilon` is negative or `NaN`.
503
+ ///
504
+ /// Special cases:
505
+ /// ```
506
+ /// equal(+0.0, -0.0, epsilon) => true for any `epsilon >= 0.0`
507
+ /// equal(-0.0, +0.0, epsilon) => true for any `epsilon >= 0.0`
508
+ /// equal(+inf, +inf, epsilon) => true for any `epsilon >= 0.0`
509
+ /// equal(-inf, -inf, epsilon) => true for any `epsilon >= 0.0`
510
+ /// equal(x, NaN, epsilon) => false for any x and `epsilon >= 0.0`
511
+ /// equal(NaN, y, epsilon) => false for any y and `epsilon >= 0.0`
512
+ /// ```
513
+ ///
514
+ /// Example:
515
+ /// ```motoko
516
+ /// import Float "mo:base/Float";
517
+ ///
518
+ /// let epsilon = 1e-6;
519
+ /// Float.equal(-12.3, -1.23e1, epsilon) // => true
520
+ /// ```
521
+ public func equalWithin(x : Float, y : Float, epsilon : Float) : Bool {
522
+ if (not (epsilon >= 0.0)) {
523
+ // also considers NaN, not identical to `epsilon < 0.0`
524
+ Prim.trap("epsilon must be greater or equal 0.0")
525
+ };
526
+ x == y or abs(x - y) <= epsilon // `x == y` to also consider infinity equal
527
+ };
528
+
529
+ /// Determines whether `x` is not equal to `y` within the defined tolerance of `epsilon`.
530
+ /// The `epsilon` considers numerical erros, see comment above.
531
+ /// Equivalent to `not equal(x, y, epsilon)`.
532
+ ///
533
+ /// Traps if `epsilon` is negative or `NaN`.
534
+ ///
535
+ /// Special cases:
536
+ /// ```
537
+ /// notEqual(+0.0, -0.0, epsilon) => false for any `epsilon >= 0.0`
538
+ /// notEqual(-0.0, +0.0, epsilon) => false for any `epsilon >= 0.0`
539
+ /// notEqual(+inf, +inf, epsilon) => false for any `epsilon >= 0.0`
540
+ /// notEqual(-inf, -inf, epsilon) => false for any `epsilon >= 0.0`
541
+ /// notEqual(x, NaN, epsilon) => true for any x and `epsilon >= 0.0`
542
+ /// notEqual(NaN, y, epsilon) => true for any y and `epsilon >= 0.0`
543
+ /// ```
544
+ ///
545
+ /// Example:
546
+ /// ```motoko
547
+ /// import Float "mo:base/Float";
548
+ ///
549
+ /// let epsilon = 1e-6;
550
+ /// Float.notEqual(-12.3, -1.23e1, epsilon) // => false
551
+ /// ```
552
+ public func notEqualWithin(x : Float, y : Float, epsilon : Float) : Bool {
553
+ not equalWithin(x, y, epsilon)
554
+ };
555
+
556
+ /// Returns `x < y`.
557
+ ///
558
+ /// Special cases:
559
+ /// ```
560
+ /// less(+0.0, -0.0) => false
561
+ /// less(-0.0, +0.0) => false
562
+ /// less(NaN, y) => false for any Float y
563
+ /// less(x, NaN) => false for any Float x
564
+ /// ```
565
+ ///
566
+ /// Example:
567
+ /// ```motoko
568
+ /// import Float "mo:base/Float";
569
+ ///
570
+ /// Float.less(Float.e, Float.pi) // => true
571
+ /// ```
572
+ public func less(x : Float, y : Float) : Bool { x < y };
573
+
574
+ /// Returns `x <= y`.
575
+ ///
576
+ /// Special cases:
577
+ /// ```
578
+ /// lessOrEqual(+0.0, -0.0) => true
579
+ /// lessOrEqual(-0.0, +0.0) => true
580
+ /// lessOrEqual(NaN, y) => false for any Float y
581
+ /// lessOrEqual(x, NaN) => false for any Float x
582
+ /// ```
583
+ ///
584
+ /// Example:
585
+ /// ```motoko
586
+ /// import Float "mo:base/Float";
587
+ ///
588
+ /// Float.lessOrEqual(0.123, 0.1234) // => true
589
+ /// ```
590
+ public func lessOrEqual(x : Float, y : Float) : Bool { x <= y };
591
+
592
+ /// Returns `x > y`.
593
+ ///
594
+ /// Special cases:
595
+ /// ```
596
+ /// greater(+0.0, -0.0) => false
597
+ /// greater(-0.0, +0.0) => false
598
+ /// greater(NaN, y) => false for any Float y
599
+ /// greater(x, NaN) => false for any Float x
600
+ /// ```
601
+ ///
602
+ /// Example:
603
+ /// ```motoko
604
+ /// import Float "mo:base/Float";
605
+ ///
606
+ /// Float.greater(Float.pi, Float.e) // => true
607
+ /// ```
608
+ public func greater(x : Float, y : Float) : Bool { x > y };
609
+
610
+ /// Returns `x >= y`.
611
+ ///
612
+ /// Special cases:
613
+ /// ```
614
+ /// greaterOrEqual(+0.0, -0.0) => true
615
+ /// greaterOrEqual(-0.0, +0.0) => true
616
+ /// greaterOrEqual(NaN, y) => false for any Float y
617
+ /// greaterOrEqual(x, NaN) => false for any Float x
618
+ /// ```
619
+ ///
620
+ /// Example:
621
+ /// ```motoko
622
+ /// import Float "mo:base/Float";
623
+ ///
624
+ /// Float.greaterOrEqual(0.1234, 0.123) // => true
625
+ /// ```
626
+ public func greaterOrEqual(x : Float, y : Float) : Bool { x >= y };
627
+
628
+ /// Defines a total order of `x` and `y` for use in sorting.
629
+ ///
630
+ /// Note: Using this operation to determine equality or inequality is discouraged for two reasons:
631
+ /// * It does not consider numerical errors, see comment above. Use `equal(x, y)` or
632
+ /// `notEqual(x, y)` to test for equality or inequality, respectively.
633
+ /// * `NaN` are here considered equal if their sign matches, which is different to the standard equality
634
+ /// by `==` or when using `equal()` or `notEqual()`.
635
+ ///
636
+ /// Total order:
637
+ /// * negative NaN (no distinction between signalling and quiet negative NaN)
638
+ /// * negative infinity
639
+ /// * negative numbers (including negative subnormal numbers in standard order)
640
+ /// * negative zero (`-0.0`)
641
+ /// * positive zero (`+0.0`)
642
+ /// * positive numbers (including positive subnormal numbers in standard order)
643
+ /// * positive infinity
644
+ /// * positive NaN (no distinction between signalling and quiet positive NaN)
645
+ ///
646
+ /// Example:
647
+ /// ```motoko
648
+ /// import Float "mo:base/Float";
649
+ ///
650
+ /// Float.compare(0.123, 0.1234) // => #less
651
+ /// ```
652
+ public func compare(x : Float, y : Float) : { #less; #equal; #greater } {
653
+ if (isNaN(x)) {
654
+ if (isNegative(x)) {
655
+ if (isNaN(y) and isNegative(y)) { #equal } else { #less }
656
+ } else {
657
+ if (isNaN(y) and not isNegative(y)) { #equal } else { #greater }
658
+ }
659
+ } else if (isNaN(y)) {
660
+ if (isNegative(y)) {
661
+ #greater
662
+ } else {
663
+ #less
664
+ }
665
+ } else {
666
+ if (x == y) { #equal } else if (x < y) { #less } else { #greater }
667
+ }
668
+ };
669
+
670
+ func isNegative(number : Float) : Bool {
671
+ copySign(1.0, number) < 0.0
672
+ };
673
+
674
+ /// Returns the negation of `x`, `-x` .
675
+ ///
676
+ /// Changes the sign bit for infinity.
677
+ /// Issue: Inconsistent behavior for zero and `NaN`. Probably related to
678
+ /// https://github.com/dfinity/motoko/issues/3646
679
+ ///
680
+ /// Example:
681
+ /// ```motoko
682
+ /// import Float "mo:base/Float";
683
+ ///
684
+ /// Float.neg(1.23) // => -1.23
685
+ /// ```
686
+ public func neg(x : Float) : Float { -x };
687
+
688
+ /// Returns the sum of `x` and `y`, `x + y`.
689
+ ///
690
+ /// Note: Numerical errors may occur, see comment above.
691
+ ///
692
+ /// Special cases:
693
+ /// ```
694
+ /// add(+inf, y) => +inf if y is any Float except -inf and NaN
695
+ /// add(-inf, y) => -inf if y is any Float except +inf and NaN
696
+ /// add(+inf, -inf) => NaN
697
+ /// add(NaN, y) => NaN for any Float y
698
+ /// ```
699
+ /// The same cases apply commutatively, i.e. for `add(y, x)`.
700
+ ///
701
+ /// Example:
702
+ /// ```motoko
703
+ /// import Float "mo:base/Float";
704
+ ///
705
+ /// Float.add(1.23, 0.123) // => 1.353
706
+ /// ```
707
+ public func add(x : Float, y : Float) : Float { x + y };
708
+
709
+ /// Returns the difference of `x` and `y`, `x - y`.
710
+ ///
711
+ /// Note: Numerical errors may occur, see comment above.
712
+ ///
713
+ /// Special cases:
714
+ /// ```
715
+ /// sub(+inf, y) => +inf if y is any Float except +inf or NaN
716
+ /// sub(-inf, y) => -inf if y is any Float except -inf and NaN
717
+ /// sub(x, +inf) => -inf if x is any Float except +inf and NaN
718
+ /// sub(x, -inf) => +inf if x is any Float except -inf and NaN
719
+ /// sub(+inf, +inf) => NaN
720
+ /// sub(-inf, -inf) => NaN
721
+ /// sub(NaN, y) => NaN for any Float y
722
+ /// sub(x, NaN) => NaN for any Float x
723
+ /// ```
724
+ ///
725
+ /// Example:
726
+ /// ```motoko
727
+ /// import Float "mo:base/Float";
728
+ ///
729
+ /// Float.sub(1.23, 0.123) // => 1.107
730
+ /// ```
731
+ public func sub(x : Float, y : Float) : Float { x - y };
732
+
733
+ /// Returns the product of `x` and `y`, `x * y`.
734
+ ///
735
+ /// Note: Numerical errors may occur, see comment above.
736
+ ///
737
+ /// Special cases:
738
+ /// ```
739
+ /// mul(+inf, y) => +inf if y > 0.0
740
+ /// mul(-inf, y) => -inf if y > 0.0
741
+ /// mul(+inf, y) => -inf if y < 0.0
742
+ /// mul(-inf, y) => +inf if y < 0.0
743
+ /// mul(+inf, 0.0) => NaN
744
+ /// mul(-inf, 0.0) => NaN
745
+ /// mul(NaN, y) => NaN for any Float y
746
+ /// ```
747
+ /// The same cases apply commutatively, i.e. for `mul(y, x)`.
748
+ ///
749
+ /// Example:
750
+ /// ```motoko
751
+ /// import Float "mo:base/Float";
752
+ ///
753
+ /// Float.mul(1.23, 1e2) // => 123.0
754
+ /// ```
755
+ public func mul(x : Float, y : Float) : Float { x * y };
756
+
757
+ /// Returns the division of `x` by `y`, `x / y`.
758
+ ///
759
+ /// Note: Numerical errors may occur, see comment above.
760
+ ///
761
+ /// Special cases:
762
+ /// ```
763
+ /// div(0.0, 0.0) => NaN
764
+ /// div(x, 0.0) => +inf for x > 0.0
765
+ /// div(x, 0.0) => -inf for x < 0.0
766
+ /// div(x, +inf) => 0.0 for any x except +inf, -inf, and NaN
767
+ /// div(x, -inf) => 0.0 for any x except +inf, -inf, and NaN
768
+ /// div(+inf, y) => +inf if y >= 0.0
769
+ /// div(+inf, y) => -inf if y < 0.0
770
+ /// div(-inf, y) => -inf if y >= 0.0
771
+ /// div(-inf, y) => +inf if y < 0.0
772
+ /// div(NaN, y) => NaN for any Float y
773
+ /// div(x, NaN) => NaN for any Float x
774
+ /// ```
775
+ ///
776
+ /// Example:
777
+ /// ```motoko
778
+ /// import Float "mo:base/Float";
779
+ ///
780
+ /// Float.div(1.23, 1e2) // => 0.0123
781
+ /// ```
782
+ public func div(x : Float, y : Float) : Float { x / y };
783
+
784
+ /// Returns the floating point division remainder `x % y`,
785
+ /// which is defined as `x - trunc(x / y) * y`.
786
+ ///
787
+ /// Note: Numerical errors may occur, see comment above.
788
+ ///
789
+ /// Special cases:
790
+ /// ```
791
+ /// rem(0.0, 0.0) => NaN
792
+ /// rem(x, y) => +inf if sign(x) == sign(y) for any x and y not being +inf, -inf, or NaN
793
+ /// rem(x, y) => -inf if sign(x) != sign(y) for any x and y not being +inf, -inf, or NaN
794
+ /// rem(x, +inf) => x for any x except +inf, -inf, and NaN
795
+ /// rem(x, -inf) => x for any x except +inf, -inf, and NaN
796
+ /// rem(+inf, y) => NaN for any Float y
797
+ /// rem(-inf, y) => NaN for any Float y
798
+ /// rem(NaN, y) => NaN for any Float y
799
+ /// rem(x, NaN) => NaN for any Float x
800
+ /// ```
801
+ ///
802
+ /// Example:
803
+ /// ```motoko
804
+ /// import Float "mo:base/Float";
805
+ ///
806
+ /// Float.rem(7.2, 2.3) // => 0.3 (with numerical imprecision)
807
+ /// ```
808
+ public func rem(x : Float, y : Float) : Float { x % y };
809
+
810
+ /// Returns `x` to the power of `y`, `x ** y`.
811
+ ///
812
+ /// Note: Numerical errors may occur, see comment above.
813
+ ///
814
+ /// Special cases:
815
+ /// ```
816
+ /// pow(+inf, y) => +inf for any y > 0.0 including +inf
817
+ /// pow(+inf, 0.0) => 1.0
818
+ /// pow(+inf, y) => 0.0 for any y < 0.0 including -inf
819
+ /// pow(x, +inf) => +inf if x > 0.0 or x < 0.0
820
+ /// pow(0.0, +inf) => 0.0
821
+ /// pow(x, -inf) => 0.0 if x > 0.0 or x < 0.0
822
+ /// pow(0.0, -inf) => +inf
823
+ /// pow(x, y) => NaN if x < 0.0 and y is a non-integral Float
824
+ /// pow(-inf, y) => +inf if y > 0.0 and y is a non-integral or an even integral Float
825
+ /// pow(-inf, y) => -inf if y > 0.0 and y is an odd integral Float
826
+ /// pow(-inf, 0.0) => 1.0
827
+ /// pow(-inf, y) => 0.0 if y < 0.0
828
+ /// pow(-inf, +inf) => +inf
829
+ /// pow(-inf, -inf) => 1.0
830
+ /// pow(NaN, y) => NaN if y != 0.0
831
+ /// pow(NaN, 0.0) => 1.0
832
+ /// pow(x, NaN) => NaN for any Float x
833
+ /// ```
834
+ ///
835
+ /// Example:
836
+ /// ```motoko
837
+ /// import Float "mo:base/Float";
838
+ ///
839
+ /// Float.pow(2.5, 2.0) // => 6.25
840
+ /// ```
841
+ public func pow(x : Float, y : Float) : Float { x ** y };
842
+
843
+ }