File tree Expand file tree Collapse file tree 3 files changed +81
-0
lines changed
Expand file tree Collapse file tree 3 files changed +81
-0
lines changed Original file line number Diff line number Diff line change @@ -65,3 +65,31 @@ from Pervasives use { (==) }
6565// Regression #1339
6666let arr = [> 1, 2, 3]
6767assert arr[toNumber(1L)] == 2
68+
69+ // pow
70+ assert 0L ** 3L == 0L
71+ assert 0L ** 2L == 0L
72+ assert 0L ** 1L == 0L
73+ assert 0L ** 0L == 1L
74+ assert 1L ** 0L == 1L
75+ assert -1L ** 0L == 1L
76+ assert 1L ** 1L == 1L
77+ assert 2L ** 1L == 2L
78+ assert 300L ** 1L == 300L
79+ assert -1L ** 1L == -1L
80+ assert -2L ** 1L == -2L
81+ assert -300L ** 1L == -300L
82+ assert 0L ** 1L == 0L
83+ assert 1L ** 0L == 1L
84+ assert 0L ** 0L == 1L
85+ assert 1L ** 5L == 1L
86+ assert 5L ** 5L == 3125L
87+ assert -5L ** 5L == -3125L
88+ assert 5L ** 6L == 15625L
89+ assert -5L ** 6L == 15625L
90+ assert 1L ** 1L == 1L
91+ assert 2L ** 1L == 2L
92+ assert 300L ** 1L == 300L
93+ assert -1L ** 1L == -1L
94+ assert -2L ** 1L == -2L
95+ assert -300L ** 1L == -300L
Original file line number Diff line number Diff line change @@ -505,3 +505,30 @@ provide let popcnt = (value: Int64) => {
505505 let ptr = newInt64(WasmI64.popcnt(nv))
506506 WasmI32.toGrain(ptr): Int64
507507}
508+
509+ // Exponentiation by squaring https://en.wikipedia.org/wiki/Exponentiation_by_squaring special path for int^int
510+ let rec expBySquaring = (y, x, n) => {
511+ if (n == 0L) {
512+ 1L
513+ } else if (n == 1L) {
514+ x * y
515+ } else if (n % 2L == 0L) {
516+ expBySquaring(y, x * x, n / 2L)
517+ } else {
518+ expBySquaring(x * y, x * x, (n - 1L) / 2L)
519+ }
520+ }
521+
522+ /**
523+ * Computes the exponentiation of the given base and power.
524+ *
525+ * @param base: The base number
526+ * @param power: The exponent number
527+ * @returns The base raised to the given power
528+ *
529+ * @since v0.6.0
530+ */
531+ provide let (**) = (base, power) => {
532+ if (power < 0L) return expBySquaring(1L, 1L / base, power * -1L)
533+ else return expBySquaring(1L, base, power)
534+ }
Original file line number Diff line number Diff line change @@ -880,3 +880,29 @@ Returns:
880880| ----| -----------|
881881| ` Int64 ` | The amount of 1-bits in its operand|
882882
883+ ### Int64.** (\*\* )**
884+
885+ <details disabled >
886+ <summary tabindex =" -1 " >Added in <code >next</code ></summary >
887+ No other changes yet.
888+ </details >
889+
890+ ``` grain
891+ (**) : (base: Int64, power: Int64) => Int64
892+ ```
893+
894+ Computes the exponentiation of the given base and power.
895+
896+ Parameters:
897+
898+ | param| type| description|
899+ | -----| ----| -----------|
900+ | ` base ` | ` Int64 ` | The base number|
901+ | ` power ` | ` Int64 ` | The exponent number|
902+
903+ Returns:
904+
905+ | type| description|
906+ | ----| -----------|
907+ | ` Int64 ` | The base raised to the given power|
908+
You can’t perform that action at this time.
0 commit comments