@agoric/zoe 0.26.3-dev-34632ea.0 → 0.26.3-dev-f0367d7.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.
@@ -1,49 +0,0 @@
1
- import { Nat } from '@endo/nat';
2
-
3
- /** @typedef {(x: number | bigint, y: number | bigint) => NatValue} NatOp */
4
-
5
- /**
6
- * These operations should be used for calculations with the values of
7
- * basic fungible tokens.
8
- *
9
- * natSafeMath is designed to be used directly, and so it needs to
10
- * validate the inputs, as well as the outputs when necessary.
11
- */
12
- export const natSafeMath = harden({
13
- /** @type {NatOp} */
14
- // BigInts don't observably overflow
15
- add: (x, y) => Nat(x) + Nat(y),
16
- /** @type {NatOp} */
17
- subtract: (x, y) => Nat(Nat(x) - Nat(y)),
18
- /** @type {NatOp} */
19
- multiply: (x, y) => Nat(x) * Nat(y),
20
- /** @type {NatOp} */
21
- floorDivide: (x, y) => Nat(x) / Nat(y),
22
- /** @type {NatOp} */
23
- ceilDivide: (x, y) => {
24
- y = Nat(y);
25
- return Nat(Nat(x) + y - 1n) / y;
26
- },
27
- /**
28
- * Divide using half-to-even (aka Banker's Rounding) as in IEEE 774 default rounding
29
- *
30
- * @type {NatOp}
31
- */
32
- bankersDivide: (a, b) => {
33
- a = Nat(a);
34
- b = Nat(b);
35
-
36
- const div = a / b;
37
- const rem = a % b;
38
- // if remainder > half divisor, should have rounded up instead of down, so add 1
39
- if (rem * 2n > b) {
40
- return div + 1n;
41
- } else if (rem * 2n === b) {
42
- // Add 1 if result is odd to get an even return value
43
- if (div % 2n === 1n) return div + 1n;
44
- }
45
- return div;
46
- },
47
- /** @type {(x: number | bigint, y: number | bigint) => boolean} */
48
- isGTE: (x, y) => Nat(x) >= Nat(y),
49
- });