Skip to content

Commit dd37ea9

Browse files
committed
wip: numerical types and math
1 parent c25c963 commit dd37ea9

File tree

1 file changed

+79
-5
lines changed

1 file changed

+79
-5
lines changed

docs/tealscript-migration.md

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ class Swapper
7777

7878
### Box Creation
7979

80-
APP_ID = TemplateVar<AppID>();
81-
8280
TODO
8381

8482
### Box Iteration
@@ -366,11 +364,87 @@ class AppCaller extends LogicSig {
366364

367365
### Numerical Types
368366

369-
TODO
367+
#### `number` Type
370368

371-
### Math and Overflows
369+
Both TEALScript and Algorand TypeScript have a `uint64` type, but Algorand TypeScript disallows any types to be resolved as `number`. This
370+
means all arithmetic values must be explicitly typed as `uint64`, otherwise they will have the `number` type which is not allowed.
372371

373-
TODO
372+
##### TEALScript
373+
374+
```ts
375+
add(a: uint64, b: uint64): uint64 {
376+
// Type not needed for sum
377+
const sum = a + b;
378+
return sum;
379+
}
380+
```
381+
382+
##### Algorand TypeScript
383+
384+
```ts
385+
add(a: uint64, b: uint64): uint64 {
386+
// The type is required for sum
387+
const sum: uint64 = a + b;
388+
return sum;
389+
}
390+
```
391+
392+
#### UintN types
393+
394+
TEALScript supports typed numeric literals for most common uint types, such as `uint8`, `uint16`, `uint256`, etc. In Algorand TypeScript,
395+
the UintN constructors must be used.
396+
397+
##### TEALScript
398+
399+
```ts
400+
addOne(n: uint256): uint256 {
401+
const one: uint256 = 1;
402+
const sum = n + one;
403+
return sum;
404+
}
405+
```
406+
407+
##### Algorand TypeScript
408+
409+
```ts
410+
addOne(n: UintN256): UintN256 {
411+
// Need to explicitly use UintN256 constructor to get uint256 and use BigUint to perform arithmetic
412+
const one = new BigUint(1);
413+
const sum = new UintN256(BigUint(n.bytes) + one + one);
414+
return sum;
415+
}
416+
```
417+
418+
#### Math and Overflows
419+
420+
In TEALScript, overflow checks do not occur until the value is encoded (returned, logged, put into an array/object). In Algorand TypeScript,
421+
overflow checking occurs whenever the `UintN` constructor is used. Since overflow checking is fairly expensive, it is recommended to not use
422+
the `UintN` type until it needs to be encoded.
423+
424+
##### TEALScript
425+
426+
```ts
427+
addToNumber(n: uint256) {
428+
assert(n != 0)
429+
const x: uint256 = 255
430+
const sum = n + x // Intermediate value of overflows the max u256, but not checked here
431+
432+
// final returned value is within max value of u256, so no error
433+
return sum - x
434+
}
435+
```
436+
437+
##### Algorand TypeScript
438+
439+
```ts
440+
addToNumber(n: UintN256) {
441+
// Use biguint for intermediate values which can go up to u512
442+
const x: biguint = 255
443+
const sum: biguint = BigUint(n.bytes) + x
444+
445+
return UinN256(sum - x)
446+
}
447+
```
374448

375449
### Casting
376450

0 commit comments

Comments
 (0)