@grain/stdlib 0.6.5 → 0.7.0

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 (139) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/LICENSE +1 -1
  3. package/README.md +2 -2
  4. package/array.gr +55 -7
  5. package/array.md +123 -77
  6. package/bigint.md +30 -30
  7. package/buffer.gr +20 -53
  8. package/buffer.md +47 -47
  9. package/bytes.gr +111 -35
  10. package/bytes.md +111 -32
  11. package/char.gr +201 -99
  12. package/char.md +361 -34
  13. package/exception.gr +11 -11
  14. package/exception.md +26 -1
  15. package/float32.gr +327 -3
  16. package/float32.md +606 -19
  17. package/float64.gr +320 -3
  18. package/float64.md +606 -19
  19. package/fs.gr +1082 -0
  20. package/fs.md +630 -0
  21. package/hash.gr +142 -88
  22. package/hash.md +102 -14
  23. package/int16.md +23 -23
  24. package/int32.gr +25 -4
  25. package/int32.md +65 -30
  26. package/int64.gr +26 -1
  27. package/int64.md +65 -30
  28. package/int8.md +23 -23
  29. package/json.gr +366 -51
  30. package/json.md +418 -2
  31. package/list.gr +387 -49
  32. package/list.md +492 -69
  33. package/map.gr +20 -12
  34. package/map.md +44 -38
  35. package/marshal.gr +41 -40
  36. package/marshal.md +2 -2
  37. package/number.gr +159 -30
  38. package/number.md +215 -38
  39. package/option.md +21 -21
  40. package/package.json +5 -3
  41. package/path.gr +48 -0
  42. package/path.md +103 -12
  43. package/pervasives.gr +2 -2
  44. package/pervasives.md +37 -37
  45. package/priorityqueue.gr +7 -7
  46. package/priorityqueue.md +19 -19
  47. package/queue.gr +183 -29
  48. package/queue.md +296 -40
  49. package/random.md +6 -6
  50. package/range.gr +4 -4
  51. package/range.md +6 -6
  52. package/rational.md +16 -16
  53. package/regex.gr +52 -51
  54. package/regex.md +11 -11
  55. package/result.md +16 -16
  56. package/runtime/atof/common.md +39 -39
  57. package/runtime/atof/decimal.gr +6 -6
  58. package/runtime/atof/decimal.md +8 -8
  59. package/runtime/atof/lemire.gr +5 -5
  60. package/runtime/atof/lemire.md +1 -1
  61. package/runtime/atof/parse.gr +16 -16
  62. package/runtime/atof/parse.md +2 -2
  63. package/runtime/atof/slow.md +1 -1
  64. package/runtime/atof/table.md +2 -2
  65. package/runtime/atoi/parse.gr +3 -3
  66. package/runtime/atoi/parse.md +1 -1
  67. package/runtime/bigint.gr +15 -47
  68. package/runtime/bigint.md +54 -60
  69. package/runtime/compare.gr +2 -2
  70. package/runtime/compare.md +1 -1
  71. package/runtime/dataStructures.md +33 -33
  72. package/runtime/debugPrint.gr +4 -1
  73. package/runtime/debugPrint.md +9 -9
  74. package/runtime/equal.gr +99 -77
  75. package/runtime/equal.md +1 -1
  76. package/runtime/exception.gr +62 -82
  77. package/runtime/exception.md +62 -11
  78. package/runtime/gc.gr +39 -45
  79. package/runtime/gc.md +4 -4
  80. package/runtime/malloc.gr +7 -7
  81. package/runtime/malloc.md +4 -4
  82. package/runtime/math/kernel/cos.gr +70 -0
  83. package/runtime/math/kernel/cos.md +14 -0
  84. package/runtime/math/kernel/sin.gr +65 -0
  85. package/runtime/math/kernel/sin.md +14 -0
  86. package/runtime/math/kernel/tan.gr +136 -0
  87. package/runtime/math/kernel/tan.md +14 -0
  88. package/runtime/math/rempio2.gr +244 -0
  89. package/runtime/math/rempio2.md +14 -0
  90. package/runtime/math/trig.gr +130 -0
  91. package/runtime/math/trig.md +28 -0
  92. package/runtime/math/umuldi.gr +26 -0
  93. package/runtime/math/umuldi.md +14 -0
  94. package/runtime/numberUtils.gr +29 -29
  95. package/runtime/numberUtils.md +12 -12
  96. package/runtime/numbers.gr +373 -381
  97. package/runtime/numbers.md +79 -73
  98. package/runtime/string.gr +37 -105
  99. package/runtime/string.md +3 -9
  100. package/runtime/unsafe/constants.md +24 -24
  101. package/runtime/unsafe/conv.md +13 -13
  102. package/runtime/unsafe/memory.gr +24 -20
  103. package/runtime/unsafe/memory.md +27 -7
  104. package/runtime/unsafe/offsets.gr +36 -0
  105. package/runtime/unsafe/offsets.md +88 -0
  106. package/runtime/unsafe/panic.gr +28 -0
  107. package/runtime/unsafe/panic.md +14 -0
  108. package/runtime/unsafe/tags.md +32 -32
  109. package/runtime/unsafe/wasmf32.md +28 -28
  110. package/runtime/unsafe/wasmf64.md +28 -28
  111. package/runtime/unsafe/wasmi32.md +47 -47
  112. package/runtime/unsafe/wasmi64.md +50 -50
  113. package/runtime/utf8.gr +189 -0
  114. package/runtime/utf8.md +117 -0
  115. package/runtime/wasi.gr +4 -2
  116. package/runtime/wasi.md +138 -138
  117. package/set.gr +18 -11
  118. package/set.md +42 -36
  119. package/stack.gr +171 -2
  120. package/stack.md +297 -15
  121. package/string.gr +352 -557
  122. package/string.md +77 -34
  123. package/uint16.gr +81 -0
  124. package/uint16.md +183 -22
  125. package/uint32.gr +25 -4
  126. package/uint32.md +63 -28
  127. package/uint64.gr +25 -5
  128. package/uint64.md +63 -28
  129. package/uint8.gr +81 -0
  130. package/uint8.md +183 -22
  131. package/uri.gr +57 -53
  132. package/uri.md +11 -12
  133. package/wasi/file.gr +67 -59
  134. package/wasi/file.md +39 -39
  135. package/wasi/process.md +5 -5
  136. package/wasi/random.md +3 -3
  137. package/wasi/time.md +4 -4
  138. package/runtime/utils/printing.gr +0 -60
  139. package/runtime/utils/printing.md +0 -26
package/float64.gr CHANGED
@@ -12,6 +12,12 @@
12
12
  */
13
13
  module Float64
14
14
 
15
+ from "runtime/unsafe/offsets" include Offsets
16
+ use Offsets.{
17
+ _FLOAT64_VALUE_OFFSET as _VALUE_OFFSET,
18
+ _INT64_VALUE_OFFSET,
19
+ _UINT64_VALUE_OFFSET,
20
+ }
15
21
  from "runtime/unsafe/wasmi32" include WasmI32
16
22
  from "runtime/unsafe/wasmf64" include WasmF64
17
23
  use WasmF64.{ (-), (+), (*), (/), (<), (<=), (>), (>=) }
@@ -23,9 +29,8 @@ use Numbers.{
23
29
  coerceNumberToFloat64 as fromNumber,
24
30
  coerceFloat64ToNumber as toNumber,
25
31
  }
26
-
27
- @unsafe
28
- let _VALUE_OFFSET = 8n
32
+ from "runtime/math/trig" include Trig
33
+ use Trig.{ sin, cos, tan }
29
34
 
30
35
  /**
31
36
  * Infinity represented as a Float64 value.
@@ -68,6 +73,42 @@ provide let e = 2.718281828459045d
68
73
 
69
74
  provide { fromNumber, toNumber }
70
75
 
76
+ /**
77
+ * Interprets an Int64 as a Float64.
78
+ *
79
+ * @param value: The value to convert
80
+ * @returns The Int64 interpreted as an Float64
81
+ *
82
+ * @example assert Float64.reinterpretInt64(4607182418800017408L) == 1.0d
83
+ * @example assert Float64.reinterpretInt64(-4616189618054758400L) == -1.0d
84
+ *
85
+ * @since v0.7.0
86
+ */
87
+ @unsafe
88
+ provide let reinterpretInt64 = (value: Int64) => {
89
+ let x = WasmF64.load(WasmI32.fromGrain(value), _INT64_VALUE_OFFSET)
90
+ let result = newFloat64(x)
91
+ WasmI32.toGrain(result): Float64
92
+ }
93
+
94
+ /**
95
+ * Interprets an Uint64 as a Float64.
96
+ *
97
+ * @param value: The value to convert
98
+ * @returns The Uint64 interpreted as an Float64
99
+ *
100
+ * @example assert Float64.reinterpretUint64(4607182418800017408uL) == 1.0d
101
+ * @example assert Float64.reinterpretUint64(13830554455654793216uL) == -1.0d
102
+ *
103
+ * @since v0.7.0
104
+ */
105
+ @unsafe
106
+ provide let reinterpretUint64 = (value: Uint64) => {
107
+ let x = WasmF64.load(WasmI32.fromGrain(value), _UINT64_VALUE_OFFSET)
108
+ let result = newFloat64(x)
109
+ WasmI32.toGrain(result): Float64
110
+ }
111
+
71
112
  /**
72
113
  * Computes the sum of its operands.
73
114
  *
@@ -156,6 +197,27 @@ provide let (/) = (x: Float64, y: Float64) => {
156
197
  WasmI32.toGrain(ptr): Float64
157
198
  }
158
199
 
200
+ /**
201
+ * Computes the exponentiation of the given base and power.
202
+ *
203
+ * @param base: The base float
204
+ * @param power: The exponent float
205
+ * @returns The base raised to the given power
206
+ *
207
+ * @example
208
+ * use Float64.{ (**) }
209
+ * assert 2.0d ** 2.0d == 4.0d
210
+ *
211
+ * @since v0.7.0
212
+ */
213
+ @unsafe
214
+ provide let (**) = (base: Float64, power: Float64) => {
215
+ let basev = WasmF64.load(WasmI32.fromGrain(base), _VALUE_OFFSET)
216
+ let powerv = WasmF64.load(WasmI32.fromGrain(power), _VALUE_OFFSET)
217
+ let ptr = newFloat64(Numbers.powf(basev, powerv))
218
+ WasmI32.toGrain(ptr): Float64
219
+ }
220
+
159
221
  /**
160
222
  * Checks if the first value is less than the second value.
161
223
  *
@@ -248,6 +310,29 @@ provide let (>=) = (x: Float64, y: Float64) => {
248
310
  xv >= yv
249
311
  }
250
312
 
313
+ /**
314
+ * Checks if a float is finite.
315
+ * All values are finite exept for NaN, infinity or negative infinity.
316
+ *
317
+ * @param x: The number to check
318
+ * @returns `true` if the value is finite or `false` otherwise
319
+ *
320
+ * @example Float64.isFinite(0.5d)
321
+ * @example Float64.isFinite(1.0d)
322
+ * @example Float64.isFinite(Infinityd) == false
323
+ * @example Float64.isFinite(-Infinityd) == false
324
+ * @example Float64.isFinite(NaNd) == false
325
+ *
326
+ * @since v0.7.0
327
+ */
328
+ @unsafe
329
+ provide let isFinite = (x: Float64) => {
330
+ // uses the fact that all finite floats minus themselves are zero
331
+ // (NaN - NaN == NaN, inf - inf == NaN,
332
+ // -inf - -inf == NaN, inf - -inf == inf, -inf - inf == -inf)
333
+ x - x == 0.0d
334
+ }
335
+
251
336
  /**
252
337
  * Checks if the value is a float NaN value (Not A Number).
253
338
  *
@@ -280,6 +365,44 @@ provide let isNaN = (x: Float64) => x != x
280
365
  */
281
366
  provide let isInfinite = (x: Float64) => x == Infinityd || x == -Infinityd
282
367
 
368
+ /**
369
+ * Returns the smaller of its operands.
370
+ *
371
+ * @param x: The first operand
372
+ * @param y: The second operand
373
+ * @returns The smaller of the two operands
374
+ *
375
+ * @example Float64.min(5.0d, 2.0d) == 2.0d
376
+ *
377
+ * @since v0.7.0
378
+ */
379
+ @unsafe
380
+ provide let min = (x: Float64, y: Float64) => {
381
+ let xv = WasmF64.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
382
+ let yv = WasmF64.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
383
+ let ptr = newFloat64(WasmF64.min(xv, yv))
384
+ WasmI32.toGrain(ptr): Float64
385
+ }
386
+
387
+ /**
388
+ * Returns the larger of its operands.
389
+ *
390
+ * @param x: The first operand
391
+ * @param y: The second operand
392
+ * @returns The larger of the two operands
393
+ *
394
+ * @example Float64.max(5.0d, 2.0d) == 5.0d
395
+ *
396
+ * @since v0.7.0
397
+ */
398
+ @unsafe
399
+ provide let max = (x: Float64, y: Float64) => {
400
+ let xv = WasmF64.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
401
+ let yv = WasmF64.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
402
+ let ptr = newFloat64(WasmF64.max(xv, yv))
403
+ WasmI32.toGrain(ptr): Float64
404
+ }
405
+
283
406
  /**
284
407
  * Returns the absolute value. That is, it returns `x` if `x` is positive or zero and the negation of `x` if `x` is negative.
285
408
  *
@@ -315,3 +438,197 @@ provide let neg = (x: Float64) => {
315
438
  let ptr = newFloat64(WasmF64.neg(xv))
316
439
  WasmI32.toGrain(ptr): Float64
317
440
  }
441
+
442
+ /**
443
+ * Rounds its operand up to the next largest whole value.
444
+ *
445
+ * @param x: The operand to ceil
446
+ * @returns The next largest whole value of the operand
447
+ *
448
+ * @example Float64.ceil(5.5d) == 6.0d
449
+ * @example Float64.ceil(-5.5d) == -5.0d
450
+ *
451
+ * @since v0.7.0
452
+ */
453
+ @unsafe
454
+ provide let ceil = (x: Float64) => {
455
+ let xv = WasmF64.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
456
+ let ptr = newFloat64(WasmF64.ceil(xv))
457
+ WasmI32.toGrain(ptr): Float64
458
+ }
459
+
460
+ /**
461
+ * Rounds its operand down to the largest whole value less than the operand.
462
+ *
463
+ * @param x: The operand to floor
464
+ * @returns The previous whole value of the operand
465
+ *
466
+ * @example Float64.floor(5.5d) == 5.0d
467
+ * @example Float64.floor(-5.5d) == -6.0d
468
+ *
469
+ * @since v0.7.0
470
+ */
471
+ @unsafe
472
+ provide let floor = (x: Float64) => {
473
+ let xv = WasmF64.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
474
+ let ptr = newFloat64(WasmF64.floor(xv))
475
+ WasmI32.toGrain(ptr): Float64
476
+ }
477
+
478
+ /**
479
+ * Returns the whole value part of its operand, removing any fractional value.
480
+ *
481
+ * @param x: The operand to truncate
482
+ * @returns The whole value part of the operand
483
+ *
484
+ * @example Float64.trunc(5.5d) == 5.0d
485
+ *
486
+ * @since v0.7.0
487
+ */
488
+ @unsafe
489
+ provide let trunc = (x: Float64) => {
490
+ let xv = WasmF64.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
491
+ let ptr = newFloat64(WasmF64.trunc(xv))
492
+ WasmI32.toGrain(ptr): Float64
493
+ }
494
+
495
+ /**
496
+ * Returns its operand rounded to its nearest integer.
497
+ *
498
+ * @param x: The operand to round
499
+ * @returns The nearest integer to the operand
500
+ *
501
+ * @example Float64.round(5.5d) == 6.0d
502
+ * @example Float64.round(5.4d) == 5.0d
503
+ * @example Float64.round(-5.5d) == -6.0d
504
+ * @example Float64.round(-5.4d) == -5.0d
505
+ *
506
+ * @since v0.7.0
507
+ */
508
+ @unsafe
509
+ provide let round = (x: Float64) => {
510
+ let xv = WasmF64.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
511
+ let ptr = newFloat64(WasmF64.nearest(xv))
512
+ WasmI32.toGrain(ptr): Float64
513
+ }
514
+
515
+ /**
516
+ * Computes the square root of its operand.
517
+ *
518
+ * @param x: The operand to square root
519
+ * @returns The square root of the operand
520
+ *
521
+ * @example Float64.sqrt(25.0d) == 5.0d
522
+ *
523
+ * @since v0.7.0
524
+ */
525
+ @unsafe
526
+ provide let sqrt = (x: Float64) => {
527
+ let xv = WasmF64.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
528
+ let ptr = newFloat64(WasmF64.sqrt(xv))
529
+ WasmI32.toGrain(ptr): Float64
530
+ }
531
+
532
+ /**
533
+ * Copys the sign of the second operand to the first operand.
534
+ *
535
+ * @param x: The operand to be modify
536
+ * @param y: The operand to copy the sign from
537
+ * @returns The first operand with the sign of the second operand
538
+ *
539
+ * @example Float64.copySign(2.0d, 1.0d) == 2.0d
540
+ * @example Float64.copySign(3.0d, -1.0d) == -3.0d
541
+ * @example Float64.copySign(-5.0d, 1.0d) == 5.0d
542
+ *
543
+ * @since v0.7.0
544
+ */
545
+ @unsafe
546
+ provide let copySign = (x: Float64, y: Float64) => {
547
+ let xv = WasmF64.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
548
+ let yv = WasmF64.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
549
+ let ptr = newFloat64(WasmF64.copySign(xv, yv))
550
+ WasmI32.toGrain(ptr): Float64
551
+ }
552
+
553
+ /**
554
+ * Determines whether two values are considered close to each other using a relative and absolute tolerance.
555
+ *
556
+ * @param a: The first value
557
+ * @param b: The second value
558
+ * @param relativeTolerance: The maximum tolerance to use relative to the larger absolute value `a` or `b`
559
+ * @param absoluteTolerance: The absolute tolerance to use, regardless of the values of `a` or `b`
560
+ * @returns `true` if the values are considered close to each other or `false` otherwise
561
+ *
562
+ * @example Float64.isClose(1.233d, 1.233d)
563
+ * @example Float64.isClose(1.233d, 1.233000001d)
564
+ * @example Float64.isClose(8.005d, 8.450d, absoluteTolerance=0.5d)
565
+ * @example Float64.isClose(4.0d, 4.1d, relativeTolerance=0.025d)
566
+ * @example Float64.isClose(1.233d, 1.24d) == false
567
+ * @example Float64.isClose(1.233d, 1.4566d) == false
568
+ * @example Float64.isClose(8.005d, 8.450d, absoluteTolerance=0.4d) == false
569
+ * @example Float64.isClose(4.0d, 4.1d, relativeTolerance=0.024d) == false
570
+ *
571
+ * @since v0.7.0
572
+ */
573
+ provide let isClose = (a, b, relativeTolerance=1e-9d, absoluteTolerance=0.0d) => {
574
+ if (a == b) {
575
+ true
576
+ } else if (isFinite(a) && isFinite(b)) {
577
+ abs(a - b)
578
+ <= max(relativeTolerance * max(abs(a), abs(b)), absoluteTolerance)
579
+ } else {
580
+ // NaN and infinities which were not equal
581
+ false
582
+ }
583
+ }
584
+
585
+ /**
586
+ * Computes the sine of a float (in radians).
587
+ *
588
+ * @param radians: The input in radians
589
+ * @returns The computed sine
590
+ *
591
+ * @example Float64.sin(0.0d) == 0.0d
592
+ *
593
+ * @since v0.7.0
594
+ */
595
+ @unsafe
596
+ provide let sin = (radians: Float64) => {
597
+ let xval = WasmF64.load(WasmI32.fromGrain(radians), _VALUE_OFFSET)
598
+ let value = sin(xval)
599
+ WasmI32.toGrain(newFloat64(value)): Float64
600
+ }
601
+
602
+ /**
603
+ * Computes the cosine of a float (in radians).
604
+ *
605
+ * @param radians: The input in radians
606
+ * @returns The computed cosine
607
+ *
608
+ * @example Float64.cos(0.0d) == 1.0d
609
+ *
610
+ * @since v0.7.0
611
+ */
612
+ @unsafe
613
+ provide let cos = (radians: Float64) => {
614
+ let xval = WasmF64.load(WasmI32.fromGrain(radians), _VALUE_OFFSET)
615
+ let value = cos(xval)
616
+ WasmI32.toGrain(newFloat64(value)): Float64
617
+ }
618
+
619
+ /**
620
+ * Computes the tangent of a number (in radians).
621
+ *
622
+ * @param radians: The input in radians
623
+ * @returns The computed tangent
624
+ *
625
+ * @example Float64.tan(0.0d) == 0.0d
626
+ *
627
+ * @since v0.7.0
628
+ */
629
+ @unsafe
630
+ provide let tan = (radians: Float64) => {
631
+ let xval = WasmF64.load(WasmI32.fromGrain(radians), _VALUE_OFFSET)
632
+ let value = tan(xval)
633
+ WasmI32.toGrain(newFloat64(value)): Float64
634
+ }