Skip to content

Commit 48de28b

Browse files
authored
feat(stdlib): Add ** operator to Int64 module (#1937)
1 parent e50866f commit 48de28b

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

compiler/test/stdlib/int64.test.gr

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,31 @@ from Pervasives use { (==) }
6565
// Regression #1339
6666
let arr = [> 1, 2, 3]
6767
assert 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

stdlib/int64.gr

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

stdlib/int64.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff 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+

0 commit comments

Comments
 (0)