Skip to content

Commit 5534179

Browse files
committed
add restriction to avoid Math.imul
1 parent 6b58c97 commit 5534179

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

text/0001-int.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,38 @@ The [`ToInt32`] behavior follows the definition in ECMA-262 as is. In action, th
6666
1. Let `number` be mathmatically $x * y$.
6767
2. Let `int32` be `fromNumber(number)`, return `int32`.
6868

69+
The `multiply(x, y)` must produce the same result as `add(x)` accumulated `y` times.
70+
71+
```res
72+
let multiply = (x, y) => {
73+
let id = 0
74+
let rec multiply = (x, y, acc) => {
75+
switch y {
76+
| 0 => acc
77+
| n => multiply(x, n - 1, add(x, acc))
78+
}
79+
}
80+
multiply(x, y, id)
81+
}
82+
```
83+
6984
### `exponentiate(x, y)`
7085

7186
1. Let `number` be mathmatically $x ^ y$.
7287
2. Let `int32` be `fromNumber(number)`, return `int32`.
7388

74-
`exponentiate(x, y)` must match the result of `multiply` accumulated `y` times.
89+
The `exponentiate(x, y)` must produce the same result as `multiply(x)` accumulated `y` times.
7590

76-
```js
77-
function exponentiate(x, y) {
78-
let int32 = 1;
79-
for (let i = 0; i < y; i++) {
80-
int32 *= x;
91+
```res
92+
let exponentiate = (x, y) => {
93+
let id = 1
94+
let rec exponentiate = (x, y, acc) => {
95+
switch y {
96+
| 0 => acc
97+
| n => exponentiate(x, n - 1, multiply(x, acc))
98+
}
8199
}
82-
return int32 | 0;
100+
exponentiate(x, y, id)
83101
}
84102
```
85103

0 commit comments

Comments
 (0)