UNPKG

9.16 kBTypeScriptView Raw
1import type { BigNumberish, BytesLike, Numeric } from "./index.js";
2/**
3 * A description of a fixed-point arithmetic field.
4 *
5 * When specifying the fixed format, the values override the default of
6 * a ``fixed128x18``, which implies a signed 128-bit value with 18
7 * decimals of precision.
8 *
9 * The alias ``fixed`` and ``ufixed`` can be used for ``fixed128x18`` and
10 * ``ufixed128x18`` respectively.
11 *
12 * When a fixed format string begins with a ``u``, it indicates the field
13 * is unsigned, so any negative values will overflow. The first number
14 * indicates the bit-width and the second number indicates the decimal
15 * precision.
16 *
17 * When a ``number`` is used for a fixed format, it indicates the number
18 * of decimal places, and the default width and signed-ness will be used.
19 *
20 * The bit-width must be byte aligned and the decimals can be at most 80.
21 */
22export type FixedFormat = number | string | {
23 signed?: boolean;
24 width?: number;
25 decimals?: number;
26};
27/**
28 * A FixedNumber represents a value over its [[FixedFormat]]
29 * arithmetic field.
30 *
31 * A FixedNumber can be used to perform math, losslessly, on
32 * values which have decmial places.
33 *
34 * A FixedNumber has a fixed bit-width to store values in, and stores all
35 * values internally by multiplying the value by 10 raised to the power of
36 * %%decimals%%.
37 *
38 * If operations are performed that cause a value to grow too high (close to
39 * positive infinity) or too low (close to negative infinity), the value
40 * is said to //overflow//.
41 *
42 * For example, an 8-bit signed value, with 0 decimals may only be within
43 * the range ``-128`` to ``127``; so ``-128 - 1`` will overflow and become
44 * ``127``. Likewise, ``127 + 1`` will overflow and become ``-127``.
45 *
46 * Many operation have a normal and //unsafe// variant. The normal variant
47 * will throw a [[NumericFaultError]] on any overflow, while the //unsafe//
48 * variant will silently allow overflow, corrupting its value value.
49 *
50 * If operations are performed that cause a value to become too small
51 * (close to zero), the value loses precison and is said to //underflow//.
52 *
53 * For example, an value with 1 decimal place may store a number as small
54 * as ``0.1``, but the value of ``0.1 / 2`` is ``0.05``, which cannot fit
55 * into 1 decimal place, so underflow occurs which means precision is lost
56 * and the value becomes ``0``.
57 *
58 * Some operations have a normal and //signalling// variant. The normal
59 * variant will silently ignore underflow, while the //signalling// variant
60 * will thow a [[NumericFaultError]] on underflow.
61 */
62export declare class FixedNumber {
63 #private;
64 /**
65 * The specific fixed-point arithmetic field for this value.
66 */
67 readonly format: string;
68 /**
69 * This is a property so console.log shows a human-meaningful value.
70 *
71 * @private
72 */
73 readonly _value: string;
74 /**
75 * @private
76 */
77 constructor(guard: any, value: bigint, format: any);
78 /**
79 * If true, negative values are permitted, otherwise only
80 * positive values and zero are allowed.
81 */
82 get signed(): boolean;
83 /**
84 * The number of bits available to store the value.
85 */
86 get width(): number;
87 /**
88 * The number of decimal places in the fixed-point arithment field.
89 */
90 get decimals(): number;
91 /**
92 * The value as an integer, based on the smallest unit the
93 * [[decimals]] allow.
94 */
95 get value(): bigint;
96 /**
97 * Returns a new [[FixedNumber]] with the result of %%this%% added
98 * to %%other%%, ignoring overflow.
99 */
100 addUnsafe(other: FixedNumber): FixedNumber;
101 /**
102 * Returns a new [[FixedNumber]] with the result of %%this%% added
103 * to %%other%%. A [[NumericFaultError]] is thrown if overflow
104 * occurs.
105 */
106 add(other: FixedNumber): FixedNumber;
107 /**
108 * Returns a new [[FixedNumber]] with the result of %%other%% subtracted
109 * from %%this%%, ignoring overflow.
110 */
111 subUnsafe(other: FixedNumber): FixedNumber;
112 /**
113 * Returns a new [[FixedNumber]] with the result of %%other%% subtracted
114 * from %%this%%. A [[NumericFaultError]] is thrown if overflow
115 * occurs.
116 */
117 sub(other: FixedNumber): FixedNumber;
118 /**
119 * Returns a new [[FixedNumber]] with the result of %%this%% multiplied
120 * by %%other%%, ignoring overflow and underflow (precision loss).
121 */
122 mulUnsafe(other: FixedNumber): FixedNumber;
123 /**
124 * Returns a new [[FixedNumber]] with the result of %%this%% multiplied
125 * by %%other%%. A [[NumericFaultError]] is thrown if overflow
126 * occurs.
127 */
128 mul(other: FixedNumber): FixedNumber;
129 /**
130 * Returns a new [[FixedNumber]] with the result of %%this%% multiplied
131 * by %%other%%. A [[NumericFaultError]] is thrown if overflow
132 * occurs or if underflow (precision loss) occurs.
133 */
134 mulSignal(other: FixedNumber): FixedNumber;
135 /**
136 * Returns a new [[FixedNumber]] with the result of %%this%% divided
137 * by %%other%%, ignoring underflow (precision loss). A
138 * [[NumericFaultError]] is thrown if overflow occurs.
139 */
140 divUnsafe(other: FixedNumber): FixedNumber;
141 /**
142 * Returns a new [[FixedNumber]] with the result of %%this%% divided
143 * by %%other%%, ignoring underflow (precision loss). A
144 * [[NumericFaultError]] is thrown if overflow occurs.
145 */
146 div(other: FixedNumber): FixedNumber;
147 /**
148 * Returns a new [[FixedNumber]] with the result of %%this%% divided
149 * by %%other%%. A [[NumericFaultError]] is thrown if underflow
150 * (precision loss) occurs.
151 */
152 divSignal(other: FixedNumber): FixedNumber;
153 /**
154 * Returns a comparison result between %%this%% and %%other%%.
155 *
156 * This is suitable for use in sorting, where ``-1`` implies %%this%%
157 * is smaller, ``1`` implies %%this%% is larger and ``0`` implies
158 * both are equal.
159 */
160 cmp(other: FixedNumber): number;
161 /**
162 * Returns true if %%other%% is equal to %%this%%.
163 */
164 eq(other: FixedNumber): boolean;
165 /**
166 * Returns true if %%other%% is less than to %%this%%.
167 */
168 lt(other: FixedNumber): boolean;
169 /**
170 * Returns true if %%other%% is less than or equal to %%this%%.
171 */
172 lte(other: FixedNumber): boolean;
173 /**
174 * Returns true if %%other%% is greater than to %%this%%.
175 */
176 gt(other: FixedNumber): boolean;
177 /**
178 * Returns true if %%other%% is greater than or equal to %%this%%.
179 */
180 gte(other: FixedNumber): boolean;
181 /**
182 * Returns a new [[FixedNumber]] which is the largest **integer**
183 * that is less than or equal to %%this%%.
184 *
185 * The decimal component of the result will always be ``0``.
186 */
187 floor(): FixedNumber;
188 /**
189 * Returns a new [[FixedNumber]] which is the smallest **integer**
190 * that is greater than or equal to %%this%%.
191 *
192 * The decimal component of the result will always be ``0``.
193 */
194 ceiling(): FixedNumber;
195 /**
196 * Returns a new [[FixedNumber]] with the decimal component
197 * rounded up on ties at %%decimals%% places.
198 */
199 round(decimals?: number): FixedNumber;
200 /**
201 * Returns true if %%this%% is equal to ``0``.
202 */
203 isZero(): boolean;
204 /**
205 * Returns true if %%this%% is less than ``0``.
206 */
207 isNegative(): boolean;
208 /**
209 * Returns the string representation of %%this%%.
210 */
211 toString(): string;
212 /**
213 * Returns a float approximation.
214 *
215 * Due to IEEE 754 precission (or lack thereof), this function
216 * can only return an approximation and most values will contain
217 * rounding errors.
218 */
219 toUnsafeFloat(): number;
220 /**
221 * Return a new [[FixedNumber]] with the same value but has had
222 * its field set to %%format%%.
223 *
224 * This will throw if the value cannot fit into %%format%%.
225 */
226 toFormat(format: FixedFormat): FixedNumber;
227 /**
228 * Creates a new [[FixedNumber]] for %%value%% divided by
229 * %%decimal%% places with %%format%%.
230 *
231 * This will throw a [[NumericFaultError]] if %%value%% (once adjusted
232 * for %%decimals%%) cannot fit in %%format%%, either due to overflow
233 * or underflow (precision loss).
234 */
235 static fromValue(_value: BigNumberish, _decimals?: Numeric, _format?: FixedFormat): FixedNumber;
236 /**
237 * Creates a new [[FixedNumber]] for %%value%% with %%format%%.
238 *
239 * This will throw a [[NumericFaultError]] if %%value%% cannot fit
240 * in %%format%%, either due to overflow or underflow (precision loss).
241 */
242 static fromString(_value: string, _format?: FixedFormat): FixedNumber;
243 /**
244 * Creates a new [[FixedNumber]] with the big-endian representation
245 * %%value%% with %%format%%.
246 *
247 * This will throw a [[NumericFaultError]] if %%value%% cannot fit
248 * in %%format%% due to overflow.
249 */
250 static fromBytes(_value: BytesLike, _format?: FixedFormat): FixedNumber;
251}
252//# sourceMappingURL=fixednumber.d.ts.map
\No newline at end of file