quantumcoin 6.14.2 → 6.14.5
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.
- package/CHANGELOG.md +442 -442
- package/FUNDING.json +10 -10
- package/LICENSE.md +21 -21
- package/README.md +132 -142
- package/SECURITY.md +34 -34
- package/dist/README.md +22 -22
- package/dist/quantumcoin.js +1127 -1324
- package/dist/quantumcoin.js.map +1 -1
- package/dist/quantumcoin.min.js +1 -1
- package/dist/quantumcoin.umd.js +1128 -1327
- package/dist/quantumcoin.umd.js.map +1 -1
- package/dist/quantumcoin.umd.min.js +1 -1
- package/dist/wordlists-extra.js +1 -1
- package/dist/wordlists-extra.js.map +1 -1
- package/dist/wordlists-extra.min.js +1 -1
- package/lib.commonjs/README.md +16 -16
- package/lib.commonjs/_version.js +1 -1
- package/lib.commonjs/crypto/signature.d.ts +3 -76
- package/lib.commonjs/crypto/signature.d.ts.map +1 -1
- package/lib.commonjs/crypto/signature.js +15 -199
- package/lib.commonjs/crypto/signature.js.map +1 -1
- package/lib.commonjs/crypto/signing-key.d.ts +1 -1
- package/lib.commonjs/crypto/signing-key.d.ts.map +1 -1
- package/lib.commonjs/crypto/signing-key.js +19 -10
- package/lib.commonjs/crypto/signing-key.js.map +1 -1
- package/lib.commonjs/package.json +12 -12
- package/lib.commonjs/providers/provider-jsonrpc.d.ts +0 -1
- package/lib.commonjs/providers/provider-jsonrpc.d.ts.map +1 -1
- package/lib.commonjs/providers/provider-jsonrpc.js +0 -1
- package/lib.commonjs/providers/provider-jsonrpc.js.map +1 -1
- package/lib.commonjs/quantumcoin.d.ts +2 -0
- package/lib.commonjs/quantumcoin.d.ts.map +1 -1
- package/lib.commonjs/quantumcoin.js +11 -5
- package/lib.commonjs/quantumcoin.js.map +1 -1
- package/lib.commonjs/transaction/address.d.ts.map +1 -1
- package/lib.commonjs/transaction/address.js +8 -3
- package/lib.commonjs/transaction/address.js.map +1 -1
- package/lib.commonjs/transaction/transaction.d.ts.map +1 -1
- package/lib.commonjs/transaction/transaction.js +7 -40
- package/lib.commonjs/transaction/transaction.js.map +1 -1
- package/lib.commonjs/wallet/json-keystore.d.ts.map +1 -1
- package/lib.commonjs/wallet/json-keystore.js +7 -7
- package/lib.commonjs/wallet/json-keystore.js.map +1 -1
- package/lib.commonjs/wallet/wallet.d.ts.map +1 -1
- package/lib.commonjs/wallet/wallet.js +2 -2
- package/lib.commonjs/wallet/wallet.js.map +1 -1
- package/lib.esm/README.md +16 -16
- package/lib.esm/_version.js +1 -1
- package/lib.esm/crypto/signature.d.ts +3 -76
- package/lib.esm/crypto/signature.d.ts.map +1 -1
- package/lib.esm/crypto/signature.js +16 -202
- package/lib.esm/crypto/signature.js.map +1 -1
- package/lib.esm/crypto/signing-key.d.ts +1 -1
- package/lib.esm/crypto/signing-key.d.ts.map +1 -1
- package/lib.esm/crypto/signing-key.js +20 -9
- package/lib.esm/crypto/signing-key.js.map +1 -1
- package/lib.esm/package.json +12 -12
- package/lib.esm/providers/provider-jsonrpc.d.ts +0 -1
- package/lib.esm/providers/provider-jsonrpc.d.ts.map +1 -1
- package/lib.esm/providers/provider-jsonrpc.js +0 -1
- package/lib.esm/providers/provider-jsonrpc.js.map +1 -1
- package/lib.esm/quantumcoin.d.ts +2 -0
- package/lib.esm/quantumcoin.d.ts.map +1 -1
- package/lib.esm/quantumcoin.js +6 -0
- package/lib.esm/quantumcoin.js.map +1 -1
- package/lib.esm/transaction/address.d.ts.map +1 -1
- package/lib.esm/transaction/address.js +8 -2
- package/lib.esm/transaction/address.js.map +1 -1
- package/lib.esm/transaction/transaction.d.ts.map +1 -1
- package/lib.esm/transaction/transaction.js +7 -40
- package/lib.esm/transaction/transaction.js.map +1 -1
- package/lib.esm/wallet/json-keystore.d.ts.map +1 -1
- package/lib.esm/wallet/json-keystore.js +11 -5
- package/lib.esm/wallet/json-keystore.js.map +1 -1
- package/lib.esm/wallet/wallet.d.ts.map +1 -1
- package/lib.esm/wallet/wallet.js +3 -1
- package/lib.esm/wallet/wallet.js.map +1 -1
- package/package.json +6 -5
- package/rollup.config.mjs +50 -50
- package/src.ts/_version.ts +1 -1
- package/src.ts/abi/abi-coder.ts +237 -237
- package/src.ts/abi/bytes32.ts +45 -45
- package/src.ts/abi/coders/abstract-coder.ts +541 -541
- package/src.ts/abi/coders/address.ts +36 -36
- package/src.ts/abi/coders/anonymous.ts +29 -29
- package/src.ts/abi/coders/array.ts +199 -199
- package/src.ts/abi/coders/boolean.ts +27 -27
- package/src.ts/abi/coders/bytes.ts +43 -43
- package/src.ts/abi/coders/fixed-bytes.ts +37 -37
- package/src.ts/abi/coders/null.ts +28 -28
- package/src.ts/abi/coders/number.ts +63 -63
- package/src.ts/abi/coders/string.ts +29 -29
- package/src.ts/abi/coders/tuple.ts +69 -69
- package/src.ts/abi/fragments.ts +1617 -1617
- package/src.ts/abi/index.ts +41 -41
- package/src.ts/abi/interface.ts +1271 -1271
- package/src.ts/abi/typed.ts +796 -796
- package/src.ts/address/address.ts +148 -148
- package/src.ts/address/checks.ts +123 -123
- package/src.ts/address/contract-address.ts +80 -80
- package/src.ts/address/index.ts +57 -57
- package/src.ts/constants/addresses.ts +8 -8
- package/src.ts/constants/hashes.ts +7 -7
- package/src.ts/constants/index.ts +16 -16
- package/src.ts/constants/numbers.ts +35 -35
- package/src.ts/constants/strings.ts +16 -16
- package/src.ts/contract/contract.ts +1120 -1120
- package/src.ts/contract/factory.ts +143 -143
- package/src.ts/contract/index.ts +31 -31
- package/src.ts/contract/types.ts +236 -236
- package/src.ts/contract/wrappers.ts +225 -225
- package/src.ts/crypto/crypto-browser.ts +64 -64
- package/src.ts/crypto/crypto.ts +4 -4
- package/src.ts/crypto/hmac.ts +51 -51
- package/src.ts/crypto/index.ts +59 -59
- package/src.ts/crypto/keccak.ts +54 -54
- package/src.ts/crypto/pbkdf2.ts +55 -55
- package/src.ts/crypto/random.ts +36 -36
- package/src.ts/crypto/ripemd160.ts +43 -43
- package/src.ts/crypto/scrypt.ts +114 -114
- package/src.ts/crypto/sha2.ts +78 -78
- package/src.ts/crypto/signature.ts +145 -349
- package/src.ts/crypto/signing-key.ts +126 -118
- package/src.ts/hash/authorization.ts +38 -38
- package/src.ts/hash/id.ts +17 -17
- package/src.ts/hash/index.ts +18 -18
- package/src.ts/hash/message.ts +51 -51
- package/src.ts/hash/namehash.ts +101 -101
- package/src.ts/hash/solidity.ts +117 -117
- package/src.ts/hash/typed-data.ts +658 -658
- package/src.ts/index.ts +12 -12
- package/src.ts/providers/abstract-provider.ts +1761 -1761
- package/src.ts/providers/abstract-signer.ts +314 -314
- package/src.ts/providers/community.ts +49 -49
- package/src.ts/providers/contracts.ts +42 -42
- package/src.ts/providers/default-provider.ts +96 -96
- package/src.ts/providers/ens-resolver.ts +606 -606
- package/src.ts/providers/format.ts +320 -320
- package/src.ts/providers/formatting.ts +418 -418
- package/src.ts/providers/index.ts +125 -125
- package/src.ts/providers/network.ts +327 -327
- package/src.ts/providers/pagination.ts +8 -8
- package/src.ts/providers/plugin-fallback.ts +35 -35
- package/src.ts/providers/plugins-network.ts +281 -281
- package/src.ts/providers/provider-browser.ts +334 -334
- package/src.ts/providers/provider-fallback.ts +801 -801
- package/src.ts/providers/provider-ipcsocket-browser.ts +3 -3
- package/src.ts/providers/provider-ipcsocket.ts +81 -81
- package/src.ts/providers/provider-jsonrpc.ts +1334 -1335
- package/src.ts/providers/provider-socket.ts +352 -352
- package/src.ts/providers/provider-websocket.ts +103 -103
- package/src.ts/providers/provider.ts +2136 -2136
- package/src.ts/providers/signer-noncemanager.ts +98 -98
- package/src.ts/providers/signer.ts +166 -166
- package/src.ts/providers/subscriber-connection.ts +74 -74
- package/src.ts/providers/subscriber-filterid.ts +199 -199
- package/src.ts/providers/subscriber-polling.ts +321 -321
- package/src.ts/providers/ws-browser.ts +11 -11
- package/src.ts/providers/ws.ts +3 -3
- package/src.ts/quantumcoin.ts +219 -211
- package/src.ts/thirdparty.d.ts +16 -16
- package/src.ts/transaction/accesslist.ts +43 -43
- package/src.ts/transaction/address.ts +35 -31
- package/src.ts/transaction/authorization.ts +14 -14
- package/src.ts/transaction/index.ts +51 -51
- package/src.ts/transaction/transaction.ts +1349 -1379
- package/src.ts/utils/base58.ts +73 -73
- package/src.ts/utils/base64-browser.ts +25 -25
- package/src.ts/utils/base64.ts +56 -56
- package/src.ts/utils/data.ts +199 -199
- package/src.ts/utils/errors.ts +793 -793
- package/src.ts/utils/events.ts +105 -105
- package/src.ts/utils/fetch.ts +970 -970
- package/src.ts/utils/fixednumber.ts +643 -643
- package/src.ts/utils/geturl-browser.ts +81 -81
- package/src.ts/utils/geturl.ts +134 -134
- package/src.ts/utils/index.ts +95 -95
- package/src.ts/utils/maths.ts +240 -240
- package/src.ts/utils/properties.ts +60 -60
- package/src.ts/utils/rlp-decode.ts +104 -104
- package/src.ts/utils/rlp-encode.ts +64 -64
- package/src.ts/utils/rlp.ts +20 -20
- package/src.ts/utils/units.ts +91 -91
- package/src.ts/utils/utf8.ts +325 -325
- package/src.ts/utils/uuid.ts +36 -36
- package/src.ts/wallet/base-wallet.ts +160 -160
- package/src.ts/wallet/index.ts +32 -32
- package/src.ts/wallet/json-keystore.ts +108 -106
- package/src.ts/wallet/utils.ts +147 -147
- package/src.ts/wallet/wallet.ts +138 -139
- package/src.ts/wordlists/bit-reader.ts +35 -35
- package/src.ts/wordlists/decode-owl.ts +58 -58
- package/src.ts/wordlists/decode-owla.ts +33 -33
- package/src.ts/wordlists/generation/encode-latin.ts +370 -370
- package/src.ts/wordlists/index.ts +26 -26
- package/src.ts/wordlists/lang-cz.ts +33 -33
- package/src.ts/wordlists/lang-en.ts +33 -33
- package/src.ts/wordlists/lang-es.ts +35 -35
- package/src.ts/wordlists/lang-fr.ts +34 -34
- package/src.ts/wordlists/lang-it.ts +33 -33
- package/src.ts/wordlists/lang-ja.ts +181 -181
- package/src.ts/wordlists/lang-ko.ts +104 -104
- package/src.ts/wordlists/lang-pt.ts +34 -34
- package/src.ts/wordlists/lang-zh.ts +112 -112
- package/src.ts/wordlists/wordlist-owl.ts +77 -77
- package/src.ts/wordlists/wordlist-owla.ts +41 -41
- package/src.ts/wordlists/wordlist.ts +59 -59
- package/src.ts/wordlists/wordlists-browser.ts +8 -8
- package/src.ts/wordlists/wordlists-extra.ts +9 -9
- package/src.ts/wordlists/wordlists.ts +38 -38
- package/dist/quantumcoin.min.js'.gz' +0 -0
- package/dist/quantumcoin.umd.min.js'.gz' +0 -0
- package/dist/wordlists-extra.min.js'.gz' +0 -0
- package/lib.commonjs/providers/provider-alchemy.d.ts +0 -50
- package/lib.commonjs/providers/provider-alchemy.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-alchemy.js +0 -151
- package/lib.commonjs/providers/provider-alchemy.js.map +0 -1
- package/lib.commonjs/providers/provider-ankr.d.ts +0 -61
- package/lib.commonjs/providers/provider-ankr.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-ankr.js +0 -137
- package/lib.commonjs/providers/provider-ankr.js.map +0 -1
- package/lib.commonjs/providers/provider-blockscout.d.ts +0 -59
- package/lib.commonjs/providers/provider-blockscout.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-blockscout.js +0 -145
- package/lib.commonjs/providers/provider-blockscout.js.map +0 -1
- package/lib.commonjs/providers/provider-chainstack.d.ts +0 -46
- package/lib.commonjs/providers/provider-chainstack.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-chainstack.js +0 -102
- package/lib.commonjs/providers/provider-chainstack.js.map +0 -1
- package/lib.commonjs/providers/provider-cloudflare.d.ts +0 -14
- package/lib.commonjs/providers/provider-cloudflare.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-cloudflare.js +0 -26
- package/lib.commonjs/providers/provider-cloudflare.js.map +0 -1
- package/lib.commonjs/providers/provider-etherscan.d.ts +0 -147
- package/lib.commonjs/providers/provider-etherscan.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-etherscan.js +0 -587
- package/lib.commonjs/providers/provider-etherscan.js.map +0 -1
- package/lib.commonjs/providers/provider-infura.d.ts +0 -101
- package/lib.commonjs/providers/provider-infura.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-infura.js +0 -206
- package/lib.commonjs/providers/provider-infura.js.map +0 -1
- package/lib.commonjs/providers/provider-pocket.d.ts +0 -54
- package/lib.commonjs/providers/provider-pocket.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-pocket.js +0 -109
- package/lib.commonjs/providers/provider-pocket.js.map +0 -1
- package/lib.commonjs/providers/provider-quicknode.d.ts +0 -59
- package/lib.commonjs/providers/provider-quicknode.d.ts.map +0 -1
- package/lib.commonjs/providers/provider-quicknode.js +0 -163
- package/lib.commonjs/providers/provider-quicknode.js.map +0 -1
- package/lib.commonjs/wallet/hdwallet.d.ts +0 -248
- package/lib.commonjs/wallet/hdwallet.d.ts.map +0 -1
- package/lib.commonjs/wallet/hdwallet.js +0 -505
- package/lib.commonjs/wallet/hdwallet.js.map +0 -1
- package/lib.commonjs/wallet/json-crowdsale.d.ts +0 -27
- package/lib.commonjs/wallet/json-crowdsale.d.ts.map +0 -1
- package/lib.commonjs/wallet/json-crowdsale.js +0 -60
- package/lib.commonjs/wallet/json-crowdsale.js.map +0 -1
- package/lib.commonjs/wallet/mnemonic.d.ts +0 -65
- package/lib.commonjs/wallet/mnemonic.d.ts.map +0 -1
- package/lib.commonjs/wallet/mnemonic.js +0 -169
- package/lib.commonjs/wallet/mnemonic.js.map +0 -1
- package/lib.commonjs/wallet/seedwallet.d.ts +0 -4
- package/lib.commonjs/wallet/seedwallet.d.ts.map +0 -1
- package/lib.commonjs/wallet/seedwallet.js +0 -8
- package/lib.commonjs/wallet/seedwallet.js.map +0 -1
- package/lib.esm/providers/provider-alchemy.d.ts +0 -50
- package/lib.esm/providers/provider-alchemy.d.ts.map +0 -1
- package/lib.esm/providers/provider-alchemy.js +0 -147
- package/lib.esm/providers/provider-alchemy.js.map +0 -1
- package/lib.esm/providers/provider-ankr.d.ts +0 -61
- package/lib.esm/providers/provider-ankr.d.ts.map +0 -1
- package/lib.esm/providers/provider-ankr.js +0 -133
- package/lib.esm/providers/provider-ankr.js.map +0 -1
- package/lib.esm/providers/provider-blockscout.d.ts +0 -59
- package/lib.esm/providers/provider-blockscout.d.ts.map +0 -1
- package/lib.esm/providers/provider-blockscout.js +0 -141
- package/lib.esm/providers/provider-blockscout.js.map +0 -1
- package/lib.esm/providers/provider-chainstack.d.ts +0 -46
- package/lib.esm/providers/provider-chainstack.d.ts.map +0 -1
- package/lib.esm/providers/provider-chainstack.js +0 -98
- package/lib.esm/providers/provider-chainstack.js.map +0 -1
- package/lib.esm/providers/provider-cloudflare.d.ts +0 -14
- package/lib.esm/providers/provider-cloudflare.d.ts.map +0 -1
- package/lib.esm/providers/provider-cloudflare.js +0 -22
- package/lib.esm/providers/provider-cloudflare.js.map +0 -1
- package/lib.esm/providers/provider-etherscan.d.ts +0 -147
- package/lib.esm/providers/provider-etherscan.d.ts.map +0 -1
- package/lib.esm/providers/provider-etherscan.js +0 -584
- package/lib.esm/providers/provider-etherscan.js.map +0 -1
- package/lib.esm/providers/provider-infura.d.ts +0 -101
- package/lib.esm/providers/provider-infura.d.ts.map +0 -1
- package/lib.esm/providers/provider-infura.js +0 -201
- package/lib.esm/providers/provider-infura.js.map +0 -1
- package/lib.esm/providers/provider-pocket.d.ts +0 -54
- package/lib.esm/providers/provider-pocket.d.ts.map +0 -1
- package/lib.esm/providers/provider-pocket.js +0 -105
- package/lib.esm/providers/provider-pocket.js.map +0 -1
- package/lib.esm/providers/provider-quicknode.d.ts +0 -59
- package/lib.esm/providers/provider-quicknode.d.ts.map +0 -1
- package/lib.esm/providers/provider-quicknode.js +0 -159
- package/lib.esm/providers/provider-quicknode.js.map +0 -1
- package/lib.esm/wallet/hdwallet.d.ts +0 -248
- package/lib.esm/wallet/hdwallet.d.ts.map +0 -1
- package/lib.esm/wallet/hdwallet.js +0 -498
- package/lib.esm/wallet/hdwallet.js.map +0 -1
- package/lib.esm/wallet/json-crowdsale.d.ts +0 -27
- package/lib.esm/wallet/json-crowdsale.d.ts.map +0 -1
- package/lib.esm/wallet/json-crowdsale.js +0 -55
- package/lib.esm/wallet/json-crowdsale.js.map +0 -1
- package/lib.esm/wallet/mnemonic.d.ts +0 -65
- package/lib.esm/wallet/mnemonic.d.ts.map +0 -1
- package/lib.esm/wallet/mnemonic.js +0 -165
- package/lib.esm/wallet/mnemonic.js.map +0 -1
- package/lib.esm/wallet/seedwallet.d.ts +0 -4
- package/lib.esm/wallet/seedwallet.d.ts.map +0 -1
- package/lib.esm/wallet/seedwallet.js +0 -4
- package/lib.esm/wallet/seedwallet.js.map +0 -1
|
@@ -1,643 +1,643 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The **FixedNumber** class permits using values with decimal places,
|
|
3
|
-
* using fixed-pont math.
|
|
4
|
-
*
|
|
5
|
-
* Fixed-point math is still based on integers under-the-hood, but uses an
|
|
6
|
-
* internal offset to store fractional components below, and each operation
|
|
7
|
-
* corrects for this after each operation.
|
|
8
|
-
*
|
|
9
|
-
* @_section: api/utils/fixed-point-math:Fixed-Point Maths [about-fixed-point-math]
|
|
10
|
-
*/
|
|
11
|
-
import { getBytes } from "./data.js";
|
|
12
|
-
import { assert, assertArgument, assertPrivate } from "./errors.js";
|
|
13
|
-
import {
|
|
14
|
-
getBigInt, getNumber, fromTwos, mask, toBigInt
|
|
15
|
-
} from "./maths.js";
|
|
16
|
-
import { defineProperties } from "./properties.js";
|
|
17
|
-
|
|
18
|
-
import type { BigNumberish, BytesLike, Numeric } from "./index.js";
|
|
19
|
-
|
|
20
|
-
const BN_N1 = BigInt(-1);
|
|
21
|
-
const BN_0 = BigInt(0);
|
|
22
|
-
const BN_1 = BigInt(1);
|
|
23
|
-
const BN_5 = BigInt(5);
|
|
24
|
-
|
|
25
|
-
const _guard = { };
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
// Constant to pull zeros from for multipliers
|
|
29
|
-
let Zeros = "0000";
|
|
30
|
-
while (Zeros.length < 80) { Zeros += Zeros; }
|
|
31
|
-
|
|
32
|
-
// Returns a string "1" followed by decimal "0"s
|
|
33
|
-
function getTens(decimals: number): bigint {
|
|
34
|
-
let result = Zeros;
|
|
35
|
-
while (result.length < decimals) { result += result; }
|
|
36
|
-
return BigInt("1" + result.substring(0, decimals));
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
/*
|
|
42
|
-
* Returns a new FixedFormat for %%value%%.
|
|
43
|
-
*
|
|
44
|
-
* If %%value%% is specified as a ``number``, the bit-width is
|
|
45
|
-
* 128 bits and %%value%% is used for the ``decimals``.
|
|
46
|
-
*
|
|
47
|
-
* A string %%value%% may begin with ``fixed`` or ``ufixed``
|
|
48
|
-
* for signed and unsigned respectfully. If no other properties
|
|
49
|
-
* are specified, the bit-width is 128-bits with 18 decimals.
|
|
50
|
-
*
|
|
51
|
-
* To specify the bit-width and demicals, append them separated
|
|
52
|
-
* by an ``"x"`` to the %%value%%.
|
|
53
|
-
*
|
|
54
|
-
* For example, ``ufixed128x18`` describes an unsigned, 128-bit
|
|
55
|
-
* wide format with 18 decimals.
|
|
56
|
-
*
|
|
57
|
-
* If %%value%% is an other object, its properties for ``signed``,
|
|
58
|
-
* ``width`` and ``decimals`` are checked.
|
|
59
|
-
*/
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* A description of a fixed-point arithmetic field.
|
|
63
|
-
*
|
|
64
|
-
* When specifying the fixed format, the values override the default of
|
|
65
|
-
* a ``fixed128x18``, which implies a signed 128-bit value with 18
|
|
66
|
-
* decimals of precision.
|
|
67
|
-
*
|
|
68
|
-
* The alias ``fixed`` and ``ufixed`` can be used for ``fixed128x18`` and
|
|
69
|
-
* ``ufixed128x18`` respectively.
|
|
70
|
-
*
|
|
71
|
-
* When a fixed format string begins with a ``u``, it indicates the field
|
|
72
|
-
* is unsigned, so any negative values will overflow. The first number
|
|
73
|
-
* indicates the bit-width and the second number indicates the decimal
|
|
74
|
-
* precision.
|
|
75
|
-
*
|
|
76
|
-
* When a ``number`` is used for a fixed format, it indicates the number
|
|
77
|
-
* of decimal places, and the default width and signed-ness will be used.
|
|
78
|
-
*
|
|
79
|
-
* The bit-width must be byte aligned and the decimals can be at most 80.
|
|
80
|
-
*/
|
|
81
|
-
export type FixedFormat = number | string | {
|
|
82
|
-
signed?: boolean,
|
|
83
|
-
width?: number,
|
|
84
|
-
decimals?: number
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
function checkValue(val: bigint, format: _FixedFormat, safeOp?: string): bigint {
|
|
88
|
-
const width = BigInt(format.width);
|
|
89
|
-
if (format.signed) {
|
|
90
|
-
const limit = (BN_1 << (width - BN_1));
|
|
91
|
-
assert(safeOp == null || (val >= -limit && val < limit), "overflow", "NUMERIC_FAULT", {
|
|
92
|
-
operation: <string>safeOp, fault: "overflow", value: val
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
if (val > BN_0) {
|
|
96
|
-
val = fromTwos(mask(val, width), width);
|
|
97
|
-
} else {
|
|
98
|
-
val = -fromTwos(mask(-val, width), width);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
} else {
|
|
102
|
-
const limit = (BN_1 << width);
|
|
103
|
-
assert(safeOp == null || (val >= 0 && val < limit), "overflow", "NUMERIC_FAULT", {
|
|
104
|
-
operation: <string>safeOp, fault: "overflow", value: val
|
|
105
|
-
});
|
|
106
|
-
val = (((val % limit) + limit) % limit) & (limit - BN_1);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return val;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
type _FixedFormat = { signed: boolean, width: number, decimals: number, name: string }
|
|
113
|
-
|
|
114
|
-
function getFormat(value?: FixedFormat): _FixedFormat {
|
|
115
|
-
if (typeof(value) === "number") { value = `fixed128x${value}` }
|
|
116
|
-
|
|
117
|
-
let signed = true;
|
|
118
|
-
let width = 128;
|
|
119
|
-
let decimals = 18;
|
|
120
|
-
|
|
121
|
-
if (typeof(value) === "string") {
|
|
122
|
-
// Parse the format string
|
|
123
|
-
if (value === "fixed") {
|
|
124
|
-
// defaults...
|
|
125
|
-
} else if (value === "ufixed") {
|
|
126
|
-
signed = false;
|
|
127
|
-
} else {
|
|
128
|
-
const match = value.match(/^(u?)fixed([0-9]+)x([0-9]+)$/);
|
|
129
|
-
assertArgument(match, "invalid fixed format", "format", value);
|
|
130
|
-
signed = (match[1] !== "u");
|
|
131
|
-
width = parseInt(match[2]);
|
|
132
|
-
decimals = parseInt(match[3]);
|
|
133
|
-
}
|
|
134
|
-
} else if (value) {
|
|
135
|
-
// Extract the values from the object
|
|
136
|
-
const v: any = value;
|
|
137
|
-
const check = (key: string, type: string, defaultValue: any): any => {
|
|
138
|
-
if (v[key] == null) { return defaultValue; }
|
|
139
|
-
assertArgument(typeof(v[key]) === type,
|
|
140
|
-
"invalid fixed format (" + key + " not " + type +")", "format." + key, v[key]);
|
|
141
|
-
return v[key];
|
|
142
|
-
}
|
|
143
|
-
signed = check("signed", "boolean", signed);
|
|
144
|
-
width = check("width", "number", width);
|
|
145
|
-
decimals = check("decimals", "number", decimals);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
assertArgument((width % 8) === 0, "invalid FixedNumber width (not byte aligned)", "format.width", width);
|
|
149
|
-
assertArgument(decimals <= 80, "invalid FixedNumber decimals (too large)", "format.decimals", decimals);
|
|
150
|
-
|
|
151
|
-
const name = (signed ? "": "u") + "fixed" + String(width) + "x" + String(decimals);
|
|
152
|
-
|
|
153
|
-
return { signed, width, decimals, name };
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function toString(val: bigint, decimals: number) {
|
|
157
|
-
let negative = "";
|
|
158
|
-
if (val < BN_0) {
|
|
159
|
-
negative = "-";
|
|
160
|
-
val *= BN_N1;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
let str = val.toString();
|
|
164
|
-
|
|
165
|
-
// No decimal point for whole values
|
|
166
|
-
if (decimals === 0) { return (negative + str); }
|
|
167
|
-
|
|
168
|
-
// Pad out to the whole component (including a whole digit)
|
|
169
|
-
while (str.length <= decimals) { str = Zeros + str; }
|
|
170
|
-
|
|
171
|
-
// Insert the decimal point
|
|
172
|
-
const index = str.length - decimals;
|
|
173
|
-
str = str.substring(0, index) + "." + str.substring(index);
|
|
174
|
-
|
|
175
|
-
// Trim the whole component (leaving at least one 0)
|
|
176
|
-
while (str[0] === "0" && str[1] !== ".") {
|
|
177
|
-
str = str.substring(1);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Trim the decimal component (leaving at least one 0)
|
|
181
|
-
while (str[str.length - 1] === "0" && str[str.length - 2] !== ".") {
|
|
182
|
-
str = str.substring(0, str.length - 1);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return (negative + str);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* A FixedNumber represents a value over its [[FixedFormat]]
|
|
191
|
-
* arithmetic field.
|
|
192
|
-
*
|
|
193
|
-
* A FixedNumber can be used to perform math, losslessly, on
|
|
194
|
-
* values which have decmial places.
|
|
195
|
-
*
|
|
196
|
-
* A FixedNumber has a fixed bit-width to store values in, and stores all
|
|
197
|
-
* values internally by multiplying the value by 10 raised to the power of
|
|
198
|
-
* %%decimals%%.
|
|
199
|
-
*
|
|
200
|
-
* If operations are performed that cause a value to grow too high (close to
|
|
201
|
-
* positive infinity) or too low (close to negative infinity), the value
|
|
202
|
-
* is said to //overflow//.
|
|
203
|
-
*
|
|
204
|
-
* For example, an 8-bit signed value, with 0 decimals may only be within
|
|
205
|
-
* the range ``-128`` to ``127``; so ``-128 - 1`` will overflow and become
|
|
206
|
-
* ``127``. Likewise, ``127 + 1`` will overflow and become ``-127``.
|
|
207
|
-
*
|
|
208
|
-
* Many operation have a normal and //unsafe// variant. The normal variant
|
|
209
|
-
* will throw a [[NumericFaultError]] on any overflow, while the //unsafe//
|
|
210
|
-
* variant will silently allow overflow, corrupting its value value.
|
|
211
|
-
*
|
|
212
|
-
* If operations are performed that cause a value to become too small
|
|
213
|
-
* (close to zero), the value loses precison and is said to //underflow//.
|
|
214
|
-
*
|
|
215
|
-
* For example, a value with 1 decimal place may store a number as small
|
|
216
|
-
* as ``0.1``, but the value of ``0.1 / 2`` is ``0.05``, which cannot fit
|
|
217
|
-
* into 1 decimal place, so underflow occurs which means precision is lost
|
|
218
|
-
* and the value becomes ``0``.
|
|
219
|
-
*
|
|
220
|
-
* Some operations have a normal and //signalling// variant. The normal
|
|
221
|
-
* variant will silently ignore underflow, while the //signalling// variant
|
|
222
|
-
* will thow a [[NumericFaultError]] on underflow.
|
|
223
|
-
*/
|
|
224
|
-
export class FixedNumber {
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* The specific fixed-point arithmetic field for this value.
|
|
228
|
-
*/
|
|
229
|
-
readonly format!: string;
|
|
230
|
-
|
|
231
|
-
readonly #format: _FixedFormat;
|
|
232
|
-
|
|
233
|
-
// The actual value (accounting for decimals)
|
|
234
|
-
#val: bigint;
|
|
235
|
-
|
|
236
|
-
// A base-10 value to multiple values by to maintain the magnitude
|
|
237
|
-
readonly #tens: bigint;
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* This is a property so console.log shows a human-meaningful value.
|
|
241
|
-
*
|
|
242
|
-
* @private
|
|
243
|
-
*/
|
|
244
|
-
readonly _value!: string;
|
|
245
|
-
|
|
246
|
-
// Use this when changing this file to get some typing info,
|
|
247
|
-
// but then switch to any to mask the internal type
|
|
248
|
-
//constructor(guard: any, value: bigint, format: _FixedFormat) {
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* @private
|
|
252
|
-
*/
|
|
253
|
-
constructor(guard: any, value: bigint, format: any) {
|
|
254
|
-
assertPrivate(guard, _guard, "FixedNumber");
|
|
255
|
-
|
|
256
|
-
this.#val = value;
|
|
257
|
-
|
|
258
|
-
this.#format = format;
|
|
259
|
-
|
|
260
|
-
const _value = toString(value, format.decimals);
|
|
261
|
-
|
|
262
|
-
defineProperties<FixedNumber>(this, { format: format.name, _value });
|
|
263
|
-
|
|
264
|
-
this.#tens = getTens(format.decimals);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* If true, negative values are permitted, otherwise only
|
|
269
|
-
* positive values and zero are allowed.
|
|
270
|
-
*/
|
|
271
|
-
get signed(): boolean { return this.#format.signed; }
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* The number of bits available to store the value.
|
|
275
|
-
*/
|
|
276
|
-
get width(): number { return this.#format.width; }
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* The number of decimal places in the fixed-point arithment field.
|
|
280
|
-
*/
|
|
281
|
-
get decimals(): number { return this.#format.decimals; }
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* The value as an integer, based on the smallest unit the
|
|
285
|
-
* [[decimals]] allow.
|
|
286
|
-
*/
|
|
287
|
-
get value(): bigint { return this.#val; }
|
|
288
|
-
|
|
289
|
-
#checkFormat(other: FixedNumber): void {
|
|
290
|
-
assertArgument(this.format === other.format,
|
|
291
|
-
"incompatible format; use fixedNumber.toFormat", "other", other);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
#checkValue(val: bigint, safeOp?: string): FixedNumber {
|
|
295
|
-
/*
|
|
296
|
-
const width = BigInt(this.width);
|
|
297
|
-
if (this.signed) {
|
|
298
|
-
const limit = (BN_1 << (width - BN_1));
|
|
299
|
-
assert(safeOp == null || (val >= -limit && val < limit), "overflow", "NUMERIC_FAULT", {
|
|
300
|
-
operation: <string>safeOp, fault: "overflow", value: val
|
|
301
|
-
});
|
|
302
|
-
|
|
303
|
-
if (val > BN_0) {
|
|
304
|
-
val = fromTwos(mask(val, width), width);
|
|
305
|
-
} else {
|
|
306
|
-
val = -fromTwos(mask(-val, width), width);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
} else {
|
|
310
|
-
const masked = mask(val, width);
|
|
311
|
-
assert(safeOp == null || (val >= 0 && val === masked), "overflow", "NUMERIC_FAULT", {
|
|
312
|
-
operation: <string>safeOp, fault: "overflow", value: val
|
|
313
|
-
});
|
|
314
|
-
val = masked;
|
|
315
|
-
}
|
|
316
|
-
*/
|
|
317
|
-
val = checkValue(val, this.#format, safeOp);
|
|
318
|
-
return new FixedNumber(_guard, val, this.#format);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
#add(o: FixedNumber, safeOp?: string): FixedNumber {
|
|
322
|
-
this.#checkFormat(o);
|
|
323
|
-
return this.#checkValue(this.#val + o.#val, safeOp);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* Returns a new [[FixedNumber]] with the result of %%this%% added
|
|
328
|
-
* to %%other%%, ignoring overflow.
|
|
329
|
-
*/
|
|
330
|
-
addUnsafe(other: FixedNumber): FixedNumber { return this.#add(other); }
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* Returns a new [[FixedNumber]] with the result of %%this%% added
|
|
334
|
-
* to %%other%%. A [[NumericFaultError]] is thrown if overflow
|
|
335
|
-
* occurs.
|
|
336
|
-
*/
|
|
337
|
-
add(other: FixedNumber): FixedNumber { return this.#add(other, "add"); }
|
|
338
|
-
|
|
339
|
-
#sub(o: FixedNumber, safeOp?: string): FixedNumber {
|
|
340
|
-
this.#checkFormat(o);
|
|
341
|
-
return this.#checkValue(this.#val - o.#val, safeOp);
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* Returns a new [[FixedNumber]] with the result of %%other%% subtracted
|
|
346
|
-
* from %%this%%, ignoring overflow.
|
|
347
|
-
*/
|
|
348
|
-
subUnsafe(other: FixedNumber): FixedNumber { return this.#sub(other); }
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* Returns a new [[FixedNumber]] with the result of %%other%% subtracted
|
|
352
|
-
* from %%this%%. A [[NumericFaultError]] is thrown if overflow
|
|
353
|
-
* occurs.
|
|
354
|
-
*/
|
|
355
|
-
sub(other: FixedNumber): FixedNumber { return this.#sub(other, "sub"); }
|
|
356
|
-
|
|
357
|
-
#mul(o: FixedNumber, safeOp?: string): FixedNumber {
|
|
358
|
-
this.#checkFormat(o);
|
|
359
|
-
return this.#checkValue((this.#val * o.#val) / this.#tens, safeOp);
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* Returns a new [[FixedNumber]] with the result of %%this%% multiplied
|
|
364
|
-
* by %%other%%, ignoring overflow and underflow (precision loss).
|
|
365
|
-
*/
|
|
366
|
-
mulUnsafe(other: FixedNumber): FixedNumber { return this.#mul(other); }
|
|
367
|
-
|
|
368
|
-
/**
|
|
369
|
-
* Returns a new [[FixedNumber]] with the result of %%this%% multiplied
|
|
370
|
-
* by %%other%%. A [[NumericFaultError]] is thrown if overflow
|
|
371
|
-
* occurs.
|
|
372
|
-
*/
|
|
373
|
-
mul(other: FixedNumber): FixedNumber { return this.#mul(other, "mul"); }
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* Returns a new [[FixedNumber]] with the result of %%this%% multiplied
|
|
377
|
-
* by %%other%%. A [[NumericFaultError]] is thrown if overflow
|
|
378
|
-
* occurs or if underflow (precision loss) occurs.
|
|
379
|
-
*/
|
|
380
|
-
mulSignal(other: FixedNumber): FixedNumber {
|
|
381
|
-
this.#checkFormat(other);
|
|
382
|
-
const value = this.#val * other.#val;
|
|
383
|
-
assert((value % this.#tens) === BN_0, "precision lost during signalling mul", "NUMERIC_FAULT", {
|
|
384
|
-
operation: "mulSignal", fault: "underflow", value: this
|
|
385
|
-
});
|
|
386
|
-
return this.#checkValue(value / this.#tens, "mulSignal");
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
#div(o: FixedNumber, safeOp?: string): FixedNumber {
|
|
390
|
-
assert(o.#val !== BN_0, "division by zero", "NUMERIC_FAULT", {
|
|
391
|
-
operation: "div", fault: "divide-by-zero", value: this
|
|
392
|
-
});
|
|
393
|
-
this.#checkFormat(o);
|
|
394
|
-
return this.#checkValue((this.#val * this.#tens) / o.#val, safeOp);
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
/**
|
|
398
|
-
* Returns a new [[FixedNumber]] with the result of %%this%% divided
|
|
399
|
-
* by %%other%%, ignoring underflow (precision loss). A
|
|
400
|
-
* [[NumericFaultError]] is thrown if overflow occurs.
|
|
401
|
-
*/
|
|
402
|
-
divUnsafe(other: FixedNumber): FixedNumber { return this.#div(other); }
|
|
403
|
-
|
|
404
|
-
/**
|
|
405
|
-
* Returns a new [[FixedNumber]] with the result of %%this%% divided
|
|
406
|
-
* by %%other%%, ignoring underflow (precision loss). A
|
|
407
|
-
* [[NumericFaultError]] is thrown if overflow occurs.
|
|
408
|
-
*/
|
|
409
|
-
div(other: FixedNumber): FixedNumber { return this.#div(other, "div"); }
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* Returns a new [[FixedNumber]] with the result of %%this%% divided
|
|
414
|
-
* by %%other%%. A [[NumericFaultError]] is thrown if underflow
|
|
415
|
-
* (precision loss) occurs.
|
|
416
|
-
*/
|
|
417
|
-
divSignal(other: FixedNumber): FixedNumber {
|
|
418
|
-
assert(other.#val !== BN_0, "division by zero", "NUMERIC_FAULT", {
|
|
419
|
-
operation: "div", fault: "divide-by-zero", value: this
|
|
420
|
-
});
|
|
421
|
-
this.#checkFormat(other);
|
|
422
|
-
const value = (this.#val * this.#tens);
|
|
423
|
-
assert((value % other.#val) === BN_0, "precision lost during signalling div", "NUMERIC_FAULT", {
|
|
424
|
-
operation: "divSignal", fault: "underflow", value: this
|
|
425
|
-
});
|
|
426
|
-
return this.#checkValue(value / other.#val, "divSignal");
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
/**
|
|
430
|
-
* Returns a comparison result between %%this%% and %%other%%.
|
|
431
|
-
*
|
|
432
|
-
* This is suitable for use in sorting, where ``-1`` implies %%this%%
|
|
433
|
-
* is smaller, ``1`` implies %%this%% is larger and ``0`` implies
|
|
434
|
-
* both are equal.
|
|
435
|
-
*/
|
|
436
|
-
cmp(other: FixedNumber): number {
|
|
437
|
-
let a = this.value, b = other.value;
|
|
438
|
-
|
|
439
|
-
// Coerce a and b to the same magnitude
|
|
440
|
-
const delta = this.decimals - other.decimals;
|
|
441
|
-
if (delta > 0) {
|
|
442
|
-
b *= getTens(delta);
|
|
443
|
-
} else if (delta < 0) {
|
|
444
|
-
a *= getTens(-delta);
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
// Comnpare
|
|
448
|
-
if (a < b) { return -1; }
|
|
449
|
-
if (a > b) { return 1; }
|
|
450
|
-
return 0;
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
/**
|
|
454
|
-
* Returns true if %%other%% is equal to %%this%%.
|
|
455
|
-
*/
|
|
456
|
-
eq(other: FixedNumber): boolean { return this.cmp(other) === 0; }
|
|
457
|
-
|
|
458
|
-
/**
|
|
459
|
-
* Returns true if %%other%% is less than to %%this%%.
|
|
460
|
-
*/
|
|
461
|
-
lt(other: FixedNumber): boolean { return this.cmp(other) < 0; }
|
|
462
|
-
|
|
463
|
-
/**
|
|
464
|
-
* Returns true if %%other%% is less than or equal to %%this%%.
|
|
465
|
-
*/
|
|
466
|
-
lte(other: FixedNumber): boolean { return this.cmp(other) <= 0; }
|
|
467
|
-
|
|
468
|
-
/**
|
|
469
|
-
* Returns true if %%other%% is greater than to %%this%%.
|
|
470
|
-
*/
|
|
471
|
-
gt(other: FixedNumber): boolean { return this.cmp(other) > 0; }
|
|
472
|
-
|
|
473
|
-
/**
|
|
474
|
-
* Returns true if %%other%% is greater than or equal to %%this%%.
|
|
475
|
-
*/
|
|
476
|
-
gte(other: FixedNumber): boolean { return this.cmp(other) >= 0; }
|
|
477
|
-
|
|
478
|
-
/**
|
|
479
|
-
* Returns a new [[FixedNumber]] which is the largest **integer**
|
|
480
|
-
* that is less than or equal to %%this%%.
|
|
481
|
-
*
|
|
482
|
-
* The decimal component of the result will always be ``0``.
|
|
483
|
-
*/
|
|
484
|
-
floor(): FixedNumber {
|
|
485
|
-
let val = this.#val;
|
|
486
|
-
if (this.#val < BN_0) { val -= this.#tens - BN_1; }
|
|
487
|
-
val = (this.#val / this.#tens) * this.#tens;
|
|
488
|
-
return this.#checkValue(val, "floor");
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
/**
|
|
492
|
-
* Returns a new [[FixedNumber]] which is the smallest **integer**
|
|
493
|
-
* that is greater than or equal to %%this%%.
|
|
494
|
-
*
|
|
495
|
-
* The decimal component of the result will always be ``0``.
|
|
496
|
-
*/
|
|
497
|
-
ceiling(): FixedNumber {
|
|
498
|
-
let val = this.#val;
|
|
499
|
-
if (this.#val > BN_0) { val += this.#tens - BN_1; }
|
|
500
|
-
val = (this.#val / this.#tens) * this.#tens;
|
|
501
|
-
return this.#checkValue(val, "ceiling");
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
/**
|
|
505
|
-
* Returns a new [[FixedNumber]] with the decimal component
|
|
506
|
-
* rounded up on ties at %%decimals%% places.
|
|
507
|
-
*/
|
|
508
|
-
round(decimals?: number): FixedNumber {
|
|
509
|
-
if (decimals == null) { decimals = 0; }
|
|
510
|
-
|
|
511
|
-
// Not enough precision to not already be rounded
|
|
512
|
-
if (decimals >= this.decimals) { return this; }
|
|
513
|
-
|
|
514
|
-
const delta = this.decimals - decimals;
|
|
515
|
-
const bump = BN_5 * getTens(delta - 1);
|
|
516
|
-
|
|
517
|
-
let value = this.value + bump;
|
|
518
|
-
const tens = getTens(delta);
|
|
519
|
-
value = (value / tens) * tens;
|
|
520
|
-
|
|
521
|
-
checkValue(value, this.#format, "round");
|
|
522
|
-
|
|
523
|
-
return new FixedNumber(_guard, value, this.#format);
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
/**
|
|
527
|
-
* Returns true if %%this%% is equal to ``0``.
|
|
528
|
-
*/
|
|
529
|
-
isZero(): boolean { return (this.#val === BN_0); }
|
|
530
|
-
|
|
531
|
-
/**
|
|
532
|
-
* Returns true if %%this%% is less than ``0``.
|
|
533
|
-
*/
|
|
534
|
-
isNegative(): boolean { return (this.#val < BN_0); }
|
|
535
|
-
|
|
536
|
-
/**
|
|
537
|
-
* Returns the string representation of %%this%%.
|
|
538
|
-
*/
|
|
539
|
-
toString(): string { return this._value; }
|
|
540
|
-
|
|
541
|
-
/**
|
|
542
|
-
* Returns a float approximation.
|
|
543
|
-
*
|
|
544
|
-
* Due to IEEE 754 precission (or lack thereof), this function
|
|
545
|
-
* can only return an approximation and most values will contain
|
|
546
|
-
* rounding errors.
|
|
547
|
-
*/
|
|
548
|
-
toUnsafeFloat(): number { return parseFloat(this.toString()); }
|
|
549
|
-
|
|
550
|
-
/**
|
|
551
|
-
* Return a new [[FixedNumber]] with the same value but has had
|
|
552
|
-
* its field set to %%format%%.
|
|
553
|
-
*
|
|
554
|
-
* This will throw if the value cannot fit into %%format%%.
|
|
555
|
-
*/
|
|
556
|
-
toFormat(format: FixedFormat): FixedNumber {
|
|
557
|
-
return FixedNumber.fromString(this.toString(), format);
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
/**
|
|
561
|
-
* Creates a new [[FixedNumber]] for %%value%% divided by
|
|
562
|
-
* %%decimal%% places with %%format%%.
|
|
563
|
-
*
|
|
564
|
-
* This will throw a [[NumericFaultError]] if %%value%% (once adjusted
|
|
565
|
-
* for %%decimals%%) cannot fit in %%format%%, either due to overflow
|
|
566
|
-
* or underflow (precision loss).
|
|
567
|
-
*/
|
|
568
|
-
static fromValue(_value: BigNumberish, _decimals?: Numeric, _format?: FixedFormat): FixedNumber {
|
|
569
|
-
const decimals = (_decimals == null) ? 0: getNumber(_decimals);
|
|
570
|
-
const format = getFormat(_format);
|
|
571
|
-
|
|
572
|
-
let value = getBigInt(_value, "value");
|
|
573
|
-
const delta = decimals - format.decimals;
|
|
574
|
-
if (delta > 0) {
|
|
575
|
-
const tens = getTens(delta);
|
|
576
|
-
assert((value % tens) === BN_0, "value loses precision for format", "NUMERIC_FAULT", {
|
|
577
|
-
operation: "fromValue", fault: "underflow", value: _value
|
|
578
|
-
});
|
|
579
|
-
value /= tens;
|
|
580
|
-
} else if (delta < 0) {
|
|
581
|
-
value *= getTens(-delta);
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
checkValue(value, format, "fromValue");
|
|
585
|
-
|
|
586
|
-
return new FixedNumber(_guard, value, format);
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
/**
|
|
590
|
-
* Creates a new [[FixedNumber]] for %%value%% with %%format%%.
|
|
591
|
-
*
|
|
592
|
-
* This will throw a [[NumericFaultError]] if %%value%% cannot fit
|
|
593
|
-
* in %%format%%, either due to overflow or underflow (precision loss).
|
|
594
|
-
*/
|
|
595
|
-
static fromString(_value: string, _format?: FixedFormat): FixedNumber {
|
|
596
|
-
const match = _value.match(/^(-?)([0-9]*)\.?([0-9]*)$/);
|
|
597
|
-
assertArgument(match && (match[2].length + match[3].length) > 0, "invalid FixedNumber string value", "value", _value);
|
|
598
|
-
|
|
599
|
-
const format = getFormat(_format);
|
|
600
|
-
|
|
601
|
-
let whole = (match[2] || "0"), decimal = (match[3] || "");
|
|
602
|
-
|
|
603
|
-
// Pad out the decimals
|
|
604
|
-
while (decimal.length < format.decimals) { decimal += Zeros; }
|
|
605
|
-
|
|
606
|
-
// Check precision is safe
|
|
607
|
-
assert(decimal.substring(format.decimals).match(/^0*$/), "too many decimals for format", "NUMERIC_FAULT", {
|
|
608
|
-
operation: "fromString", fault: "underflow", value: _value
|
|
609
|
-
});
|
|
610
|
-
|
|
611
|
-
// Remove extra padding
|
|
612
|
-
decimal = decimal.substring(0, format.decimals);
|
|
613
|
-
|
|
614
|
-
const value = BigInt(match[1] + whole + decimal)
|
|
615
|
-
|
|
616
|
-
checkValue(value, format, "fromString");
|
|
617
|
-
|
|
618
|
-
return new FixedNumber(_guard, value, format);
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
/**
|
|
622
|
-
* Creates a new [[FixedNumber]] with the big-endian representation
|
|
623
|
-
* %%value%% with %%format%%.
|
|
624
|
-
*
|
|
625
|
-
* This will throw a [[NumericFaultError]] if %%value%% cannot fit
|
|
626
|
-
* in %%format%% due to overflow.
|
|
627
|
-
*/
|
|
628
|
-
static fromBytes(_value: BytesLike, _format?: FixedFormat): FixedNumber {
|
|
629
|
-
let value = toBigInt(getBytes(_value, "value"));
|
|
630
|
-
const format = getFormat(_format);
|
|
631
|
-
|
|
632
|
-
if (format.signed) { value = fromTwos(value, format.width); }
|
|
633
|
-
|
|
634
|
-
checkValue(value, format, "fromBytes");
|
|
635
|
-
|
|
636
|
-
return new FixedNumber(_guard, value, format);
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
//const f1 = FixedNumber.fromString("12.56", "fixed16x2");
|
|
641
|
-
//const f2 = FixedNumber.fromString("0.3", "fixed16x2");
|
|
642
|
-
//console.log(f1.divSignal(f2));
|
|
643
|
-
//const BUMP = FixedNumber.from("0.5");
|
|
1
|
+
/**
|
|
2
|
+
* The **FixedNumber** class permits using values with decimal places,
|
|
3
|
+
* using fixed-pont math.
|
|
4
|
+
*
|
|
5
|
+
* Fixed-point math is still based on integers under-the-hood, but uses an
|
|
6
|
+
* internal offset to store fractional components below, and each operation
|
|
7
|
+
* corrects for this after each operation.
|
|
8
|
+
*
|
|
9
|
+
* @_section: api/utils/fixed-point-math:Fixed-Point Maths [about-fixed-point-math]
|
|
10
|
+
*/
|
|
11
|
+
import { getBytes } from "./data.js";
|
|
12
|
+
import { assert, assertArgument, assertPrivate } from "./errors.js";
|
|
13
|
+
import {
|
|
14
|
+
getBigInt, getNumber, fromTwos, mask, toBigInt
|
|
15
|
+
} from "./maths.js";
|
|
16
|
+
import { defineProperties } from "./properties.js";
|
|
17
|
+
|
|
18
|
+
import type { BigNumberish, BytesLike, Numeric } from "./index.js";
|
|
19
|
+
|
|
20
|
+
const BN_N1 = BigInt(-1);
|
|
21
|
+
const BN_0 = BigInt(0);
|
|
22
|
+
const BN_1 = BigInt(1);
|
|
23
|
+
const BN_5 = BigInt(5);
|
|
24
|
+
|
|
25
|
+
const _guard = { };
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
// Constant to pull zeros from for multipliers
|
|
29
|
+
let Zeros = "0000";
|
|
30
|
+
while (Zeros.length < 80) { Zeros += Zeros; }
|
|
31
|
+
|
|
32
|
+
// Returns a string "1" followed by decimal "0"s
|
|
33
|
+
function getTens(decimals: number): bigint {
|
|
34
|
+
let result = Zeros;
|
|
35
|
+
while (result.length < decimals) { result += result; }
|
|
36
|
+
return BigInt("1" + result.substring(0, decimals));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
/*
|
|
42
|
+
* Returns a new FixedFormat for %%value%%.
|
|
43
|
+
*
|
|
44
|
+
* If %%value%% is specified as a ``number``, the bit-width is
|
|
45
|
+
* 128 bits and %%value%% is used for the ``decimals``.
|
|
46
|
+
*
|
|
47
|
+
* A string %%value%% may begin with ``fixed`` or ``ufixed``
|
|
48
|
+
* for signed and unsigned respectfully. If no other properties
|
|
49
|
+
* are specified, the bit-width is 128-bits with 18 decimals.
|
|
50
|
+
*
|
|
51
|
+
* To specify the bit-width and demicals, append them separated
|
|
52
|
+
* by an ``"x"`` to the %%value%%.
|
|
53
|
+
*
|
|
54
|
+
* For example, ``ufixed128x18`` describes an unsigned, 128-bit
|
|
55
|
+
* wide format with 18 decimals.
|
|
56
|
+
*
|
|
57
|
+
* If %%value%% is an other object, its properties for ``signed``,
|
|
58
|
+
* ``width`` and ``decimals`` are checked.
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A description of a fixed-point arithmetic field.
|
|
63
|
+
*
|
|
64
|
+
* When specifying the fixed format, the values override the default of
|
|
65
|
+
* a ``fixed128x18``, which implies a signed 128-bit value with 18
|
|
66
|
+
* decimals of precision.
|
|
67
|
+
*
|
|
68
|
+
* The alias ``fixed`` and ``ufixed`` can be used for ``fixed128x18`` and
|
|
69
|
+
* ``ufixed128x18`` respectively.
|
|
70
|
+
*
|
|
71
|
+
* When a fixed format string begins with a ``u``, it indicates the field
|
|
72
|
+
* is unsigned, so any negative values will overflow. The first number
|
|
73
|
+
* indicates the bit-width and the second number indicates the decimal
|
|
74
|
+
* precision.
|
|
75
|
+
*
|
|
76
|
+
* When a ``number`` is used for a fixed format, it indicates the number
|
|
77
|
+
* of decimal places, and the default width and signed-ness will be used.
|
|
78
|
+
*
|
|
79
|
+
* The bit-width must be byte aligned and the decimals can be at most 80.
|
|
80
|
+
*/
|
|
81
|
+
export type FixedFormat = number | string | {
|
|
82
|
+
signed?: boolean,
|
|
83
|
+
width?: number,
|
|
84
|
+
decimals?: number
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
function checkValue(val: bigint, format: _FixedFormat, safeOp?: string): bigint {
|
|
88
|
+
const width = BigInt(format.width);
|
|
89
|
+
if (format.signed) {
|
|
90
|
+
const limit = (BN_1 << (width - BN_1));
|
|
91
|
+
assert(safeOp == null || (val >= -limit && val < limit), "overflow", "NUMERIC_FAULT", {
|
|
92
|
+
operation: <string>safeOp, fault: "overflow", value: val
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
if (val > BN_0) {
|
|
96
|
+
val = fromTwos(mask(val, width), width);
|
|
97
|
+
} else {
|
|
98
|
+
val = -fromTwos(mask(-val, width), width);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
} else {
|
|
102
|
+
const limit = (BN_1 << width);
|
|
103
|
+
assert(safeOp == null || (val >= 0 && val < limit), "overflow", "NUMERIC_FAULT", {
|
|
104
|
+
operation: <string>safeOp, fault: "overflow", value: val
|
|
105
|
+
});
|
|
106
|
+
val = (((val % limit) + limit) % limit) & (limit - BN_1);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return val;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
type _FixedFormat = { signed: boolean, width: number, decimals: number, name: string }
|
|
113
|
+
|
|
114
|
+
function getFormat(value?: FixedFormat): _FixedFormat {
|
|
115
|
+
if (typeof(value) === "number") { value = `fixed128x${value}` }
|
|
116
|
+
|
|
117
|
+
let signed = true;
|
|
118
|
+
let width = 128;
|
|
119
|
+
let decimals = 18;
|
|
120
|
+
|
|
121
|
+
if (typeof(value) === "string") {
|
|
122
|
+
// Parse the format string
|
|
123
|
+
if (value === "fixed") {
|
|
124
|
+
// defaults...
|
|
125
|
+
} else if (value === "ufixed") {
|
|
126
|
+
signed = false;
|
|
127
|
+
} else {
|
|
128
|
+
const match = value.match(/^(u?)fixed([0-9]+)x([0-9]+)$/);
|
|
129
|
+
assertArgument(match, "invalid fixed format", "format", value);
|
|
130
|
+
signed = (match[1] !== "u");
|
|
131
|
+
width = parseInt(match[2]);
|
|
132
|
+
decimals = parseInt(match[3]);
|
|
133
|
+
}
|
|
134
|
+
} else if (value) {
|
|
135
|
+
// Extract the values from the object
|
|
136
|
+
const v: any = value;
|
|
137
|
+
const check = (key: string, type: string, defaultValue: any): any => {
|
|
138
|
+
if (v[key] == null) { return defaultValue; }
|
|
139
|
+
assertArgument(typeof(v[key]) === type,
|
|
140
|
+
"invalid fixed format (" + key + " not " + type +")", "format." + key, v[key]);
|
|
141
|
+
return v[key];
|
|
142
|
+
}
|
|
143
|
+
signed = check("signed", "boolean", signed);
|
|
144
|
+
width = check("width", "number", width);
|
|
145
|
+
decimals = check("decimals", "number", decimals);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
assertArgument((width % 8) === 0, "invalid FixedNumber width (not byte aligned)", "format.width", width);
|
|
149
|
+
assertArgument(decimals <= 80, "invalid FixedNumber decimals (too large)", "format.decimals", decimals);
|
|
150
|
+
|
|
151
|
+
const name = (signed ? "": "u") + "fixed" + String(width) + "x" + String(decimals);
|
|
152
|
+
|
|
153
|
+
return { signed, width, decimals, name };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function toString(val: bigint, decimals: number) {
|
|
157
|
+
let negative = "";
|
|
158
|
+
if (val < BN_0) {
|
|
159
|
+
negative = "-";
|
|
160
|
+
val *= BN_N1;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
let str = val.toString();
|
|
164
|
+
|
|
165
|
+
// No decimal point for whole values
|
|
166
|
+
if (decimals === 0) { return (negative + str); }
|
|
167
|
+
|
|
168
|
+
// Pad out to the whole component (including a whole digit)
|
|
169
|
+
while (str.length <= decimals) { str = Zeros + str; }
|
|
170
|
+
|
|
171
|
+
// Insert the decimal point
|
|
172
|
+
const index = str.length - decimals;
|
|
173
|
+
str = str.substring(0, index) + "." + str.substring(index);
|
|
174
|
+
|
|
175
|
+
// Trim the whole component (leaving at least one 0)
|
|
176
|
+
while (str[0] === "0" && str[1] !== ".") {
|
|
177
|
+
str = str.substring(1);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Trim the decimal component (leaving at least one 0)
|
|
181
|
+
while (str[str.length - 1] === "0" && str[str.length - 2] !== ".") {
|
|
182
|
+
str = str.substring(0, str.length - 1);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return (negative + str);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* A FixedNumber represents a value over its [[FixedFormat]]
|
|
191
|
+
* arithmetic field.
|
|
192
|
+
*
|
|
193
|
+
* A FixedNumber can be used to perform math, losslessly, on
|
|
194
|
+
* values which have decmial places.
|
|
195
|
+
*
|
|
196
|
+
* A FixedNumber has a fixed bit-width to store values in, and stores all
|
|
197
|
+
* values internally by multiplying the value by 10 raised to the power of
|
|
198
|
+
* %%decimals%%.
|
|
199
|
+
*
|
|
200
|
+
* If operations are performed that cause a value to grow too high (close to
|
|
201
|
+
* positive infinity) or too low (close to negative infinity), the value
|
|
202
|
+
* is said to //overflow//.
|
|
203
|
+
*
|
|
204
|
+
* For example, an 8-bit signed value, with 0 decimals may only be within
|
|
205
|
+
* the range ``-128`` to ``127``; so ``-128 - 1`` will overflow and become
|
|
206
|
+
* ``127``. Likewise, ``127 + 1`` will overflow and become ``-127``.
|
|
207
|
+
*
|
|
208
|
+
* Many operation have a normal and //unsafe// variant. The normal variant
|
|
209
|
+
* will throw a [[NumericFaultError]] on any overflow, while the //unsafe//
|
|
210
|
+
* variant will silently allow overflow, corrupting its value value.
|
|
211
|
+
*
|
|
212
|
+
* If operations are performed that cause a value to become too small
|
|
213
|
+
* (close to zero), the value loses precison and is said to //underflow//.
|
|
214
|
+
*
|
|
215
|
+
* For example, a value with 1 decimal place may store a number as small
|
|
216
|
+
* as ``0.1``, but the value of ``0.1 / 2`` is ``0.05``, which cannot fit
|
|
217
|
+
* into 1 decimal place, so underflow occurs which means precision is lost
|
|
218
|
+
* and the value becomes ``0``.
|
|
219
|
+
*
|
|
220
|
+
* Some operations have a normal and //signalling// variant. The normal
|
|
221
|
+
* variant will silently ignore underflow, while the //signalling// variant
|
|
222
|
+
* will thow a [[NumericFaultError]] on underflow.
|
|
223
|
+
*/
|
|
224
|
+
export class FixedNumber {
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* The specific fixed-point arithmetic field for this value.
|
|
228
|
+
*/
|
|
229
|
+
readonly format!: string;
|
|
230
|
+
|
|
231
|
+
readonly #format: _FixedFormat;
|
|
232
|
+
|
|
233
|
+
// The actual value (accounting for decimals)
|
|
234
|
+
#val: bigint;
|
|
235
|
+
|
|
236
|
+
// A base-10 value to multiple values by to maintain the magnitude
|
|
237
|
+
readonly #tens: bigint;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* This is a property so console.log shows a human-meaningful value.
|
|
241
|
+
*
|
|
242
|
+
* @private
|
|
243
|
+
*/
|
|
244
|
+
readonly _value!: string;
|
|
245
|
+
|
|
246
|
+
// Use this when changing this file to get some typing info,
|
|
247
|
+
// but then switch to any to mask the internal type
|
|
248
|
+
//constructor(guard: any, value: bigint, format: _FixedFormat) {
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* @private
|
|
252
|
+
*/
|
|
253
|
+
constructor(guard: any, value: bigint, format: any) {
|
|
254
|
+
assertPrivate(guard, _guard, "FixedNumber");
|
|
255
|
+
|
|
256
|
+
this.#val = value;
|
|
257
|
+
|
|
258
|
+
this.#format = format;
|
|
259
|
+
|
|
260
|
+
const _value = toString(value, format.decimals);
|
|
261
|
+
|
|
262
|
+
defineProperties<FixedNumber>(this, { format: format.name, _value });
|
|
263
|
+
|
|
264
|
+
this.#tens = getTens(format.decimals);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* If true, negative values are permitted, otherwise only
|
|
269
|
+
* positive values and zero are allowed.
|
|
270
|
+
*/
|
|
271
|
+
get signed(): boolean { return this.#format.signed; }
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* The number of bits available to store the value.
|
|
275
|
+
*/
|
|
276
|
+
get width(): number { return this.#format.width; }
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* The number of decimal places in the fixed-point arithment field.
|
|
280
|
+
*/
|
|
281
|
+
get decimals(): number { return this.#format.decimals; }
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* The value as an integer, based on the smallest unit the
|
|
285
|
+
* [[decimals]] allow.
|
|
286
|
+
*/
|
|
287
|
+
get value(): bigint { return this.#val; }
|
|
288
|
+
|
|
289
|
+
#checkFormat(other: FixedNumber): void {
|
|
290
|
+
assertArgument(this.format === other.format,
|
|
291
|
+
"incompatible format; use fixedNumber.toFormat", "other", other);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
#checkValue(val: bigint, safeOp?: string): FixedNumber {
|
|
295
|
+
/*
|
|
296
|
+
const width = BigInt(this.width);
|
|
297
|
+
if (this.signed) {
|
|
298
|
+
const limit = (BN_1 << (width - BN_1));
|
|
299
|
+
assert(safeOp == null || (val >= -limit && val < limit), "overflow", "NUMERIC_FAULT", {
|
|
300
|
+
operation: <string>safeOp, fault: "overflow", value: val
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
if (val > BN_0) {
|
|
304
|
+
val = fromTwos(mask(val, width), width);
|
|
305
|
+
} else {
|
|
306
|
+
val = -fromTwos(mask(-val, width), width);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
} else {
|
|
310
|
+
const masked = mask(val, width);
|
|
311
|
+
assert(safeOp == null || (val >= 0 && val === masked), "overflow", "NUMERIC_FAULT", {
|
|
312
|
+
operation: <string>safeOp, fault: "overflow", value: val
|
|
313
|
+
});
|
|
314
|
+
val = masked;
|
|
315
|
+
}
|
|
316
|
+
*/
|
|
317
|
+
val = checkValue(val, this.#format, safeOp);
|
|
318
|
+
return new FixedNumber(_guard, val, this.#format);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
#add(o: FixedNumber, safeOp?: string): FixedNumber {
|
|
322
|
+
this.#checkFormat(o);
|
|
323
|
+
return this.#checkValue(this.#val + o.#val, safeOp);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Returns a new [[FixedNumber]] with the result of %%this%% added
|
|
328
|
+
* to %%other%%, ignoring overflow.
|
|
329
|
+
*/
|
|
330
|
+
addUnsafe(other: FixedNumber): FixedNumber { return this.#add(other); }
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Returns a new [[FixedNumber]] with the result of %%this%% added
|
|
334
|
+
* to %%other%%. A [[NumericFaultError]] is thrown if overflow
|
|
335
|
+
* occurs.
|
|
336
|
+
*/
|
|
337
|
+
add(other: FixedNumber): FixedNumber { return this.#add(other, "add"); }
|
|
338
|
+
|
|
339
|
+
#sub(o: FixedNumber, safeOp?: string): FixedNumber {
|
|
340
|
+
this.#checkFormat(o);
|
|
341
|
+
return this.#checkValue(this.#val - o.#val, safeOp);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Returns a new [[FixedNumber]] with the result of %%other%% subtracted
|
|
346
|
+
* from %%this%%, ignoring overflow.
|
|
347
|
+
*/
|
|
348
|
+
subUnsafe(other: FixedNumber): FixedNumber { return this.#sub(other); }
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Returns a new [[FixedNumber]] with the result of %%other%% subtracted
|
|
352
|
+
* from %%this%%. A [[NumericFaultError]] is thrown if overflow
|
|
353
|
+
* occurs.
|
|
354
|
+
*/
|
|
355
|
+
sub(other: FixedNumber): FixedNumber { return this.#sub(other, "sub"); }
|
|
356
|
+
|
|
357
|
+
#mul(o: FixedNumber, safeOp?: string): FixedNumber {
|
|
358
|
+
this.#checkFormat(o);
|
|
359
|
+
return this.#checkValue((this.#val * o.#val) / this.#tens, safeOp);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Returns a new [[FixedNumber]] with the result of %%this%% multiplied
|
|
364
|
+
* by %%other%%, ignoring overflow and underflow (precision loss).
|
|
365
|
+
*/
|
|
366
|
+
mulUnsafe(other: FixedNumber): FixedNumber { return this.#mul(other); }
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Returns a new [[FixedNumber]] with the result of %%this%% multiplied
|
|
370
|
+
* by %%other%%. A [[NumericFaultError]] is thrown if overflow
|
|
371
|
+
* occurs.
|
|
372
|
+
*/
|
|
373
|
+
mul(other: FixedNumber): FixedNumber { return this.#mul(other, "mul"); }
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Returns a new [[FixedNumber]] with the result of %%this%% multiplied
|
|
377
|
+
* by %%other%%. A [[NumericFaultError]] is thrown if overflow
|
|
378
|
+
* occurs or if underflow (precision loss) occurs.
|
|
379
|
+
*/
|
|
380
|
+
mulSignal(other: FixedNumber): FixedNumber {
|
|
381
|
+
this.#checkFormat(other);
|
|
382
|
+
const value = this.#val * other.#val;
|
|
383
|
+
assert((value % this.#tens) === BN_0, "precision lost during signalling mul", "NUMERIC_FAULT", {
|
|
384
|
+
operation: "mulSignal", fault: "underflow", value: this
|
|
385
|
+
});
|
|
386
|
+
return this.#checkValue(value / this.#tens, "mulSignal");
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
#div(o: FixedNumber, safeOp?: string): FixedNumber {
|
|
390
|
+
assert(o.#val !== BN_0, "division by zero", "NUMERIC_FAULT", {
|
|
391
|
+
operation: "div", fault: "divide-by-zero", value: this
|
|
392
|
+
});
|
|
393
|
+
this.#checkFormat(o);
|
|
394
|
+
return this.#checkValue((this.#val * this.#tens) / o.#val, safeOp);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Returns a new [[FixedNumber]] with the result of %%this%% divided
|
|
399
|
+
* by %%other%%, ignoring underflow (precision loss). A
|
|
400
|
+
* [[NumericFaultError]] is thrown if overflow occurs.
|
|
401
|
+
*/
|
|
402
|
+
divUnsafe(other: FixedNumber): FixedNumber { return this.#div(other); }
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Returns a new [[FixedNumber]] with the result of %%this%% divided
|
|
406
|
+
* by %%other%%, ignoring underflow (precision loss). A
|
|
407
|
+
* [[NumericFaultError]] is thrown if overflow occurs.
|
|
408
|
+
*/
|
|
409
|
+
div(other: FixedNumber): FixedNumber { return this.#div(other, "div"); }
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Returns a new [[FixedNumber]] with the result of %%this%% divided
|
|
414
|
+
* by %%other%%. A [[NumericFaultError]] is thrown if underflow
|
|
415
|
+
* (precision loss) occurs.
|
|
416
|
+
*/
|
|
417
|
+
divSignal(other: FixedNumber): FixedNumber {
|
|
418
|
+
assert(other.#val !== BN_0, "division by zero", "NUMERIC_FAULT", {
|
|
419
|
+
operation: "div", fault: "divide-by-zero", value: this
|
|
420
|
+
});
|
|
421
|
+
this.#checkFormat(other);
|
|
422
|
+
const value = (this.#val * this.#tens);
|
|
423
|
+
assert((value % other.#val) === BN_0, "precision lost during signalling div", "NUMERIC_FAULT", {
|
|
424
|
+
operation: "divSignal", fault: "underflow", value: this
|
|
425
|
+
});
|
|
426
|
+
return this.#checkValue(value / other.#val, "divSignal");
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Returns a comparison result between %%this%% and %%other%%.
|
|
431
|
+
*
|
|
432
|
+
* This is suitable for use in sorting, where ``-1`` implies %%this%%
|
|
433
|
+
* is smaller, ``1`` implies %%this%% is larger and ``0`` implies
|
|
434
|
+
* both are equal.
|
|
435
|
+
*/
|
|
436
|
+
cmp(other: FixedNumber): number {
|
|
437
|
+
let a = this.value, b = other.value;
|
|
438
|
+
|
|
439
|
+
// Coerce a and b to the same magnitude
|
|
440
|
+
const delta = this.decimals - other.decimals;
|
|
441
|
+
if (delta > 0) {
|
|
442
|
+
b *= getTens(delta);
|
|
443
|
+
} else if (delta < 0) {
|
|
444
|
+
a *= getTens(-delta);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Comnpare
|
|
448
|
+
if (a < b) { return -1; }
|
|
449
|
+
if (a > b) { return 1; }
|
|
450
|
+
return 0;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Returns true if %%other%% is equal to %%this%%.
|
|
455
|
+
*/
|
|
456
|
+
eq(other: FixedNumber): boolean { return this.cmp(other) === 0; }
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Returns true if %%other%% is less than to %%this%%.
|
|
460
|
+
*/
|
|
461
|
+
lt(other: FixedNumber): boolean { return this.cmp(other) < 0; }
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Returns true if %%other%% is less than or equal to %%this%%.
|
|
465
|
+
*/
|
|
466
|
+
lte(other: FixedNumber): boolean { return this.cmp(other) <= 0; }
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Returns true if %%other%% is greater than to %%this%%.
|
|
470
|
+
*/
|
|
471
|
+
gt(other: FixedNumber): boolean { return this.cmp(other) > 0; }
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Returns true if %%other%% is greater than or equal to %%this%%.
|
|
475
|
+
*/
|
|
476
|
+
gte(other: FixedNumber): boolean { return this.cmp(other) >= 0; }
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Returns a new [[FixedNumber]] which is the largest **integer**
|
|
480
|
+
* that is less than or equal to %%this%%.
|
|
481
|
+
*
|
|
482
|
+
* The decimal component of the result will always be ``0``.
|
|
483
|
+
*/
|
|
484
|
+
floor(): FixedNumber {
|
|
485
|
+
let val = this.#val;
|
|
486
|
+
if (this.#val < BN_0) { val -= this.#tens - BN_1; }
|
|
487
|
+
val = (this.#val / this.#tens) * this.#tens;
|
|
488
|
+
return this.#checkValue(val, "floor");
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Returns a new [[FixedNumber]] which is the smallest **integer**
|
|
493
|
+
* that is greater than or equal to %%this%%.
|
|
494
|
+
*
|
|
495
|
+
* The decimal component of the result will always be ``0``.
|
|
496
|
+
*/
|
|
497
|
+
ceiling(): FixedNumber {
|
|
498
|
+
let val = this.#val;
|
|
499
|
+
if (this.#val > BN_0) { val += this.#tens - BN_1; }
|
|
500
|
+
val = (this.#val / this.#tens) * this.#tens;
|
|
501
|
+
return this.#checkValue(val, "ceiling");
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Returns a new [[FixedNumber]] with the decimal component
|
|
506
|
+
* rounded up on ties at %%decimals%% places.
|
|
507
|
+
*/
|
|
508
|
+
round(decimals?: number): FixedNumber {
|
|
509
|
+
if (decimals == null) { decimals = 0; }
|
|
510
|
+
|
|
511
|
+
// Not enough precision to not already be rounded
|
|
512
|
+
if (decimals >= this.decimals) { return this; }
|
|
513
|
+
|
|
514
|
+
const delta = this.decimals - decimals;
|
|
515
|
+
const bump = BN_5 * getTens(delta - 1);
|
|
516
|
+
|
|
517
|
+
let value = this.value + bump;
|
|
518
|
+
const tens = getTens(delta);
|
|
519
|
+
value = (value / tens) * tens;
|
|
520
|
+
|
|
521
|
+
checkValue(value, this.#format, "round");
|
|
522
|
+
|
|
523
|
+
return new FixedNumber(_guard, value, this.#format);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Returns true if %%this%% is equal to ``0``.
|
|
528
|
+
*/
|
|
529
|
+
isZero(): boolean { return (this.#val === BN_0); }
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Returns true if %%this%% is less than ``0``.
|
|
533
|
+
*/
|
|
534
|
+
isNegative(): boolean { return (this.#val < BN_0); }
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Returns the string representation of %%this%%.
|
|
538
|
+
*/
|
|
539
|
+
toString(): string { return this._value; }
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Returns a float approximation.
|
|
543
|
+
*
|
|
544
|
+
* Due to IEEE 754 precission (or lack thereof), this function
|
|
545
|
+
* can only return an approximation and most values will contain
|
|
546
|
+
* rounding errors.
|
|
547
|
+
*/
|
|
548
|
+
toUnsafeFloat(): number { return parseFloat(this.toString()); }
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Return a new [[FixedNumber]] with the same value but has had
|
|
552
|
+
* its field set to %%format%%.
|
|
553
|
+
*
|
|
554
|
+
* This will throw if the value cannot fit into %%format%%.
|
|
555
|
+
*/
|
|
556
|
+
toFormat(format: FixedFormat): FixedNumber {
|
|
557
|
+
return FixedNumber.fromString(this.toString(), format);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Creates a new [[FixedNumber]] for %%value%% divided by
|
|
562
|
+
* %%decimal%% places with %%format%%.
|
|
563
|
+
*
|
|
564
|
+
* This will throw a [[NumericFaultError]] if %%value%% (once adjusted
|
|
565
|
+
* for %%decimals%%) cannot fit in %%format%%, either due to overflow
|
|
566
|
+
* or underflow (precision loss).
|
|
567
|
+
*/
|
|
568
|
+
static fromValue(_value: BigNumberish, _decimals?: Numeric, _format?: FixedFormat): FixedNumber {
|
|
569
|
+
const decimals = (_decimals == null) ? 0: getNumber(_decimals);
|
|
570
|
+
const format = getFormat(_format);
|
|
571
|
+
|
|
572
|
+
let value = getBigInt(_value, "value");
|
|
573
|
+
const delta = decimals - format.decimals;
|
|
574
|
+
if (delta > 0) {
|
|
575
|
+
const tens = getTens(delta);
|
|
576
|
+
assert((value % tens) === BN_0, "value loses precision for format", "NUMERIC_FAULT", {
|
|
577
|
+
operation: "fromValue", fault: "underflow", value: _value
|
|
578
|
+
});
|
|
579
|
+
value /= tens;
|
|
580
|
+
} else if (delta < 0) {
|
|
581
|
+
value *= getTens(-delta);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
checkValue(value, format, "fromValue");
|
|
585
|
+
|
|
586
|
+
return new FixedNumber(_guard, value, format);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Creates a new [[FixedNumber]] for %%value%% with %%format%%.
|
|
591
|
+
*
|
|
592
|
+
* This will throw a [[NumericFaultError]] if %%value%% cannot fit
|
|
593
|
+
* in %%format%%, either due to overflow or underflow (precision loss).
|
|
594
|
+
*/
|
|
595
|
+
static fromString(_value: string, _format?: FixedFormat): FixedNumber {
|
|
596
|
+
const match = _value.match(/^(-?)([0-9]*)\.?([0-9]*)$/);
|
|
597
|
+
assertArgument(match && (match[2].length + match[3].length) > 0, "invalid FixedNumber string value", "value", _value);
|
|
598
|
+
|
|
599
|
+
const format = getFormat(_format);
|
|
600
|
+
|
|
601
|
+
let whole = (match[2] || "0"), decimal = (match[3] || "");
|
|
602
|
+
|
|
603
|
+
// Pad out the decimals
|
|
604
|
+
while (decimal.length < format.decimals) { decimal += Zeros; }
|
|
605
|
+
|
|
606
|
+
// Check precision is safe
|
|
607
|
+
assert(decimal.substring(format.decimals).match(/^0*$/), "too many decimals for format", "NUMERIC_FAULT", {
|
|
608
|
+
operation: "fromString", fault: "underflow", value: _value
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
// Remove extra padding
|
|
612
|
+
decimal = decimal.substring(0, format.decimals);
|
|
613
|
+
|
|
614
|
+
const value = BigInt(match[1] + whole + decimal)
|
|
615
|
+
|
|
616
|
+
checkValue(value, format, "fromString");
|
|
617
|
+
|
|
618
|
+
return new FixedNumber(_guard, value, format);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Creates a new [[FixedNumber]] with the big-endian representation
|
|
623
|
+
* %%value%% with %%format%%.
|
|
624
|
+
*
|
|
625
|
+
* This will throw a [[NumericFaultError]] if %%value%% cannot fit
|
|
626
|
+
* in %%format%% due to overflow.
|
|
627
|
+
*/
|
|
628
|
+
static fromBytes(_value: BytesLike, _format?: FixedFormat): FixedNumber {
|
|
629
|
+
let value = toBigInt(getBytes(_value, "value"));
|
|
630
|
+
const format = getFormat(_format);
|
|
631
|
+
|
|
632
|
+
if (format.signed) { value = fromTwos(value, format.width); }
|
|
633
|
+
|
|
634
|
+
checkValue(value, format, "fromBytes");
|
|
635
|
+
|
|
636
|
+
return new FixedNumber(_guard, value, format);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
//const f1 = FixedNumber.fromString("12.56", "fixed16x2");
|
|
641
|
+
//const f2 = FixedNumber.fromString("0.3", "fixed16x2");
|
|
642
|
+
//console.log(f1.divSignal(f2));
|
|
643
|
+
//const BUMP = FixedNumber.from("0.5");
|