All files / ethers.js/src.ts/utils fixednumber.ts

78.69% Statements 506/643
67.64% Branches 23/34
25% Functions 11/44
78.69% Lines 506/643

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 6441x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 188x 188x 188x 188x 188x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 188x 188x 188x 188x 188x 188x 188x 188x 188x 124x 188x 64x 64x 188x 188x             188x 188x 188x 1x 1x 1x 188x 188x 188x 188x 188x 188x 188x 188x                         188x 188x 188x 188x 564x 376x 376x 376x 376x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 1x 188x 188x 188x 64x 64x 64x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 8056x 8056x 188x 188x 188x 2568x 2568x 188x 188x 188x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x       188x 188x                                                   188x 188x       188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x       188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x       188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x               188x 188x             188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x                     188x 188x 188x 188x 188x 188x 188x 188x 188x                               188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x           188x 188x 188x 188x 188x 188x 188x 188x           188x 188x 188x 188x 188x 188x                                 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 188x     188x 188x 188x 188x 188x 188x 188x 188x 188x 188x 94x 94x 94x 94x 94x 94x           94x     94x 94x 94x 94x 94x 188x 188x 188x 188x 188x 188x 188x 188x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 188x 188x 188x 188x 188x 188x 188x 188x 188x                   188x 1x 1x 1x 1x 1x  
/**
 *  The **FixedNumber** class permits using values with decimal places,
 *  using fixed-pont math.
 *
 *  Fixed-point math is still based on integers under-the-hood, but uses an
 *  internal offset to store fractional components below, and each operation
 *  corrects for this after each operation.
 *
 *  @_section: api/utils/fixed-point-math:Fixed-Point Maths  [about-fixed-point-math]
 */
import { getBytes } from "./data.js";
import { assert, assertArgument, assertPrivate } from "./errors.js";
import {
    getBigInt, getNumber, fromTwos, mask, toBigInt
} from "./maths.js";
import { defineProperties } from "./properties.js";
 
import type { BigNumberish, BytesLike, Numeric } from "./index.js";
 
const BN_N1 = BigInt(-1);
const BN_0 = BigInt(0);
const BN_1 = BigInt(1);
const BN_5 = BigInt(5);
 
const _guard = { };
 
 
// Constant to pull zeros from for multipliers
let Zeros = "0000";
while (Zeros.length < 80) { Zeros += Zeros; }
 
// Returns a string "1" followed by decimal "0"s
function getTens(decimals: number): bigint {
    let result = Zeros;
    while (result.length < decimals) { result += result; }
    return BigInt("1" + result.substring(0, decimals));
}
 
 
 
    /*
     *  Returns a new FixedFormat for %%value%%.
     *
     *  If %%value%% is specified as a ``number``, the bit-width is
     *  128 bits and %%value%% is used for the ``decimals``.
     *
     *  A string %%value%% may begin with ``fixed`` or ``ufixed``
     *  for signed and unsigned respectfully. If no other properties
     *  are specified, the bit-width is 128-bits with 18 decimals.
     *
     *  To specify the bit-width and demicals, append them separated
     *  by an ``"x"`` to the %%value%%.
     *
     *  For example, ``ufixed128x18`` describes an unsigned, 128-bit
     *  wide format with 18 decimals.
     *
     *  If %%value%% is an other object, its properties for ``signed``,
     *  ``width`` and ``decimals`` are checked.
     */
 
/**
 *  A description of a fixed-point arithmetic field.
 *
 *  When specifying the fixed format, the values override the default of
 *  a ``fixed128x18``, which implies a signed 128-bit value with 18
 *  decimals of precision.
 *
 *  The alias ``fixed`` and ``ufixed`` can be used for ``fixed128x18`` and
 *  ``ufixed128x18`` respectively.
 *
 *  When a fixed format string begins with a ``u``, it indicates the field
 *  is unsigned, so any negative values will overflow. The first number
 *  indicates the bit-width and the second number indicates the decimal
 *  precision.
 *
 *  When a ``number`` is used for a fixed format, it indicates the number
 *  of decimal places, and the default width and signed-ness will be used.
 *
 *  The bit-width must be byte aligned and the decimals can be at most 80.
 */
export type FixedFormat = number | string | {
    signed?: boolean,
    width?: number,
    decimals?: number
};
 
function checkValue(val: bigint, format: _FixedFormat, safeOp?: string): bigint {
    const width = BigInt(format.width);
    if (format.signed) {
        const limit = (BN_1 << (width - BN_1));
        assert(safeOp == null || (val >= -limit  && val < limit), "overflow", "NUMERIC_FAULT", {
            operation: <string>safeOp, fault: "overflow", value: val
        });
 
        if (val > BN_0) {
            val = fromTwos(mask(val, width), width);
        } else {
            val = -fromTwos(mask(-val, width), width);
        }
 
    } else {
        const limit = (BN_1 << width);
        assert(safeOp == null || (val >= 0 && val < limit), "overflow", "NUMERIC_FAULT", {
            operation: <string>safeOp, fault: "overflow", value: val
        });
        val = (((val % limit) + limit) % limit) & (limit - BN_1);
    }
 
    return val;
}
 
type _FixedFormat = { signed: boolean, width: number, decimals: number, name: string }
 
function getFormat(value?: FixedFormat): _FixedFormat {
    if (typeof(value) === "number") { value = `fixed128x${value}` }
 
    let signed = true;
    let width = 128;
    let decimals = 18;
 
    if (typeof(value) === "string") {
        // Parse the format string
        if (value === "fixed") {
            // defaults...
        } else if (value === "ufixed") {
            signed = false;
        } else {
            const match = value.match(/^(u?)fixed([0-9]+)x([0-9]+)$/);
            assertArgument(match, "invalid fixed format", "format", value);
            signed = (match[1] !== "u");
            width = parseInt(match[2]);
            decimals = parseInt(match[3]);
        }
    } else if (value) {
        // Extract the values from the object
        const v: any = value;
        const check = (key: string, type: string, defaultValue: any): any => {
            if (v[key] == null) { return defaultValue; }
            assertArgument(typeof(v[key]) === type,
                "invalid fixed format (" + key + " not " + type +")", "format." + key, v[key]);
            return v[key];
        }
        signed = check("signed", "boolean", signed);
        width = check("width", "number", width);
        decimals = check("decimals", "number", decimals);
    }
 
    assertArgument((width % 8) === 0, "invalid FixedNumber width (not byte aligned)", "format.width", width);
    assertArgument(decimals <= 80, "invalid FixedNumber decimals (too large)", "format.decimals", decimals);
 
    const name = (signed ? "": "u") + "fixed" + String(width) + "x" + String(decimals);
 
    return { signed, width, decimals, name };
}
 
function toString(val: bigint, decimals: number) {
    let negative = "";
    if (val < BN_0) {
        negative = "-";
        val *= BN_N1;
    }
 
    let str = val.toString();
 
    // No decimal point for whole values
    if (decimals === 0) { return (negative + str); }
 
    // Pad out to the whole component (including a whole digit)
    while (str.length <= decimals) { str = Zeros + str; }
 
    // Insert the decimal point
    const index = str.length - decimals;
    str = str.substring(0, index) + "." + str.substring(index);
 
    // Trim the whole component (leaving at least one 0)
    while (str[0] === "0" && str[1] !== ".") {
        str = str.substring(1);
    }
 
    // Trim the decimal component (leaving at least one 0)
    while (str[str.length - 1] === "0" && str[str.length - 2] !== ".") {
        str = str.substring(0, str.length - 1);
    }
 
    return (negative + str);
}
 
 
/**
 *  A FixedNumber represents a value over its [[FixedFormat]]
 *  arithmetic field.
 *
 *  A FixedNumber can be used to perform math, losslessly, on
 *  values which have decmial places.
 *
 *  A FixedNumber has a fixed bit-width to store values in, and stores all
 *  values internally by multiplying the value by 10 raised to the power of
 *  %%decimals%%.
 *
 *  If operations are performed that cause a value to grow too high (close to
 *  positive infinity) or too low (close to negative infinity), the value
 *  is said to //overflow//.
 *
 *  For example, an 8-bit signed value, with 0 decimals may only be within
 *  the range ``-128`` to ``127``; so ``-128 - 1`` will overflow and become
 *  ``127``. Likewise, ``127 + 1`` will overflow and become ``-127``.
 *
 *  Many operation have a normal and //unsafe// variant. The normal variant
 *  will throw a [[NumericFaultError]] on any overflow, while the //unsafe//
 *  variant will silently allow overflow, corrupting its value value.
 *
 *  If operations are performed that cause a value to become too small
 *  (close to zero), the value loses precison and is said to //underflow//.
 *
 *  For example, a value with 1 decimal place may store a number as small
 *  as ``0.1``, but the value of ``0.1 / 2`` is ``0.05``, which cannot fit
 *  into 1 decimal place, so underflow occurs which means precision is lost
 *  and the value becomes ``0``.
 *
 *  Some operations have a normal and //signalling// variant. The normal
 *  variant will silently ignore underflow, while the //signalling// variant
 *  will thow a [[NumericFaultError]] on underflow.
 */
export class FixedNumber {
 
    /**
     *  The specific fixed-point arithmetic field for this value.
     */
    readonly format!: string;
 
    readonly #format: _FixedFormat;
 
    // The actual value (accounting for decimals)
    #val: bigint;
 
    // A base-10 value to multiple values by to maintain the magnitude
    readonly #tens: bigint;
 
    /**
     *  This is a property so console.log shows a human-meaningful value.
     *
     *  @private
     */
    readonly _value!: string;
 
    // Use this when changing this file to get some typing info,
    // but then switch to any to mask the internal type
    //constructor(guard: any, value: bigint, format: _FixedFormat) {
 
    /**
     *  @private
     */
    constructor(guard: any, value: bigint, format: any) {
        assertPrivate(guard, _guard, "FixedNumber");
 
        this.#val = value;
 
        this.#format = format;
 
        const _value = toString(value, format.decimals);
 
        defineProperties<FixedNumber>(this, { format: format.name, _value });
 
        this.#tens = getTens(format.decimals);
    }
 
    /**
     *  If true, negative values are permitted, otherwise only
     *  positive values and zero are allowed.
     */
    get signed(): boolean { return this.#format.signed; }
 
    /**
     *  The number of bits available to store the value.
     */
    get width(): number { return this.#format.width; }
 
    /**
     *  The number of decimal places in the fixed-point arithment field.
     */
    get decimals(): number { return this.#format.decimals; }
 
    /**
     *  The value as an integer, based on the smallest unit the
     *  [[decimals]] allow.
     */
    get value(): bigint { return this.#val; }
 
    #checkFormat(other: FixedNumber): void {
        assertArgument(this.format === other.format,
            "incompatible format; use fixedNumber.toFormat", "other", other);
    }
 
    #checkValue(val: bigint, safeOp?: string): FixedNumber {
/*
        const width = BigInt(this.width);
        if (this.signed) {
            const limit = (BN_1 << (width - BN_1));
            assert(safeOp == null || (val >= -limit  && val < limit), "overflow", "NUMERIC_FAULT", {
                operation: <string>safeOp, fault: "overflow", value: val
            });

            if (val > BN_0) {
                val = fromTwos(mask(val, width), width);
            } else {
                val = -fromTwos(mask(-val, width), width);
            }

        } else {
            const masked = mask(val, width);
            assert(safeOp == null || (val >= 0 && val === masked), "overflow", "NUMERIC_FAULT", {
                operation: <string>safeOp, fault: "overflow", value: val
            });
            val = masked;
        }
*/
        val = checkValue(val, this.#format, safeOp);
        return new FixedNumber(_guard, val, this.#format);
    }
 
    #add(o: FixedNumber, safeOp?: string): FixedNumber {
        this.#checkFormat(o);
        return this.#checkValue(this.#val + o.#val, safeOp);
    }
 
    /**
     *  Returns a new [[FixedNumber]] with the result of %%this%% added
     *  to %%other%%, ignoring overflow.
     */
    addUnsafe(other: FixedNumber): FixedNumber { return this.#add(other); }
 
    /**
     *  Returns a new [[FixedNumber]] with the result of %%this%% added
     *  to %%other%%. A [[NumericFaultError]] is thrown if overflow
     *  occurs.
     */
    add(other: FixedNumber): FixedNumber { return this.#add(other, "add"); }
 
    #sub(o: FixedNumber, safeOp?: string): FixedNumber {
        this.#checkFormat(o);
        return this.#checkValue(this.#val - o.#val, safeOp);
    }
 
    /**
     *  Returns a new [[FixedNumber]] with the result of %%other%% subtracted
     *  from %%this%%, ignoring overflow.
     */
    subUnsafe(other: FixedNumber): FixedNumber { return this.#sub(other); }
 
    /**
     *  Returns a new [[FixedNumber]] with the result of %%other%% subtracted
     *  from %%this%%. A [[NumericFaultError]] is thrown if overflow
     *  occurs.
     */
    sub(other: FixedNumber): FixedNumber { return this.#sub(other, "sub"); }
 
    #mul(o: FixedNumber, safeOp?: string): FixedNumber {
        this.#checkFormat(o);
        return this.#checkValue((this.#val * o.#val) / this.#tens, safeOp);
    }
 
    /**
     *  Returns a new [[FixedNumber]] with the result of %%this%% multiplied
     *  by %%other%%, ignoring overflow and underflow (precision loss).
     */
    mulUnsafe(other: FixedNumber): FixedNumber { return this.#mul(other); }
 
    /**
     *  Returns a new [[FixedNumber]] with the result of %%this%% multiplied
     *  by %%other%%. A [[NumericFaultError]] is thrown if overflow
     *  occurs.
     */
    mul(other: FixedNumber): FixedNumber { return this.#mul(other, "mul"); }
 
    /**
     *  Returns a new [[FixedNumber]] with the result of %%this%% multiplied
     *  by %%other%%. A [[NumericFaultError]] is thrown if overflow
     *  occurs or if underflow (precision loss) occurs.
     */
    mulSignal(other: FixedNumber): FixedNumber {
        this.#checkFormat(other);
        const value = this.#val * other.#val;
        assert((value % this.#tens) === BN_0, "precision lost during signalling mul", "NUMERIC_FAULT", {
            operation: "mulSignal", fault: "underflow", value: this
        });
        return this.#checkValue(value / this.#tens, "mulSignal");
    }
 
    #div(o: FixedNumber, safeOp?: string): FixedNumber {
        assert(o.#val !== BN_0, "division by zero", "NUMERIC_FAULT", {
            operation: "div", fault: "divide-by-zero", value: this
        });
        this.#checkFormat(o);
        return this.#checkValue((this.#val * this.#tens) / o.#val, safeOp);
    }
 
    /**
     *  Returns a new [[FixedNumber]] with the result of %%this%% divided
     *  by %%other%%, ignoring underflow (precision loss). A
     *  [[NumericFaultError]] is thrown if overflow occurs.
     */
    divUnsafe(other: FixedNumber): FixedNumber { return this.#div(other); }
 
    /**
     *  Returns a new [[FixedNumber]] with the result of %%this%% divided
     *  by %%other%%, ignoring underflow (precision loss). A
     *  [[NumericFaultError]] is thrown if overflow occurs.
     */
    div(other: FixedNumber): FixedNumber { return this.#div(other, "div"); }
 
 
    /**
     *  Returns a new [[FixedNumber]] with the result of %%this%% divided
     *  by %%other%%. A [[NumericFaultError]] is thrown if underflow
     *  (precision loss) occurs.
     */
    divSignal(other: FixedNumber): FixedNumber {
        assert(other.#val !== BN_0, "division by zero", "NUMERIC_FAULT", {
            operation: "div", fault: "divide-by-zero", value: this
        });
        this.#checkFormat(other);
        const value = (this.#val * this.#tens);
        assert((value % other.#val) === BN_0, "precision lost during signalling div", "NUMERIC_FAULT", {
            operation: "divSignal", fault: "underflow", value: this
        });
        return this.#checkValue(value / other.#val, "divSignal");
    }
 
    /**
     *  Returns a comparison result between %%this%% and %%other%%.
     *
     *  This is suitable for use in sorting, where ``-1`` implies %%this%%
     *  is smaller, ``1`` implies %%this%% is larger and ``0`` implies
     *  both are equal.
     */
     cmp(other: FixedNumber): number {
         let a = this.value, b = other.value;

         // Coerce a and b to the same magnitude
         const delta = this.decimals - other.decimals;
         if (delta > 0) {
             b *= getTens(delta);
         } else if (delta < 0) {
             a *= getTens(-delta);
         }

         // Comnpare
         if (a < b) { return -1; }
         if (a > b) { return 1; }
         return 0;
     }
 
    /**
     *  Returns true if %%other%% is equal to %%this%%.
     */
     eq(other: FixedNumber): boolean { return this.cmp(other) === 0; }
 
    /**
     *  Returns true if %%other%% is less than to %%this%%.
     */
     lt(other: FixedNumber): boolean { return this.cmp(other) < 0; }
 
    /**
     *  Returns true if %%other%% is less than or equal to %%this%%.
     */
     lte(other: FixedNumber): boolean { return this.cmp(other) <= 0; }
 
    /**
     *  Returns true if %%other%% is greater than to %%this%%.
     */
     gt(other: FixedNumber): boolean { return this.cmp(other) > 0; }
 
    /**
     *  Returns true if %%other%% is greater than or equal to %%this%%.
     */
     gte(other: FixedNumber): boolean { return this.cmp(other) >= 0; }
 
    /**
     *  Returns a new [[FixedNumber]] which is the largest **integer**
     *  that is less than or equal to %%this%%.
     *
     *  The decimal component of the result will always be ``0``.
     */
    floor(): FixedNumber {
        let val = this.#val;
        if (this.#val < BN_0) { val -= this.#tens - BN_1; }
        val = (this.#val / this.#tens) * this.#tens;
        return this.#checkValue(val, "floor");
    }
 
    /**
     *  Returns a new [[FixedNumber]] which is the smallest **integer**
     *  that is greater than or equal to %%this%%.
     *
     *  The decimal component of the result will always be ``0``.
     */
    ceiling(): FixedNumber {
        let val = this.#val;
        if (this.#val > BN_0) { val += this.#tens - BN_1; }
        val = (this.#val / this.#tens) * this.#tens;
        return this.#checkValue(val, "ceiling");
    }
 
    /**
     *  Returns a new [[FixedNumber]] with the decimal component
     *  rounded up on ties at %%decimals%% places.
     */
    round(decimals?: number): FixedNumber {
        if (decimals == null) { decimals = 0; }

        // Not enough precision to not already be rounded
        if (decimals >= this.decimals) { return this; }

        const delta = this.decimals - decimals;
        const bump = BN_5 * getTens(delta - 1);

        let value = this.value + bump;
        const tens = getTens(delta);
        value = (value / tens) * tens;

        checkValue(value, this.#format, "round");

        return new FixedNumber(_guard, value, this.#format);
    }
 
    /**
     *  Returns true if %%this%% is equal to ``0``.
     */
    isZero(): boolean { return (this.#val === BN_0); }
 
    /**
     *  Returns true if %%this%% is less than ``0``.
     */
    isNegative(): boolean { return (this.#val < BN_0); }
 
    /**
     *  Returns the string representation of %%this%%.
     */
    toString(): string { return this._value; }
 
    /**
     *  Returns a float approximation.
     *
     *  Due to IEEE 754 precission (or lack thereof), this function
     *  can only return an approximation and most values will contain
     *  rounding errors.
     */
    toUnsafeFloat(): number { return parseFloat(this.toString()); }
 
    /**
     *  Return a new [[FixedNumber]] with the same value but has had
     *  its field set to %%format%%.
     *
     *  This will throw if the value cannot fit into %%format%%.
     */
    toFormat(format: FixedFormat): FixedNumber {
        return FixedNumber.fromString(this.toString(), format);
    }
 
    /**
     *  Creates a new [[FixedNumber]] for %%value%% divided by
     *  %%decimal%% places with %%format%%.
     *
     *  This will throw a [[NumericFaultError]] if %%value%% (once adjusted
     *  for %%decimals%%) cannot fit in %%format%%, either due to overflow
     *  or underflow (precision loss).
     */
    static fromValue(_value: BigNumberish, _decimals?: Numeric, _format?: FixedFormat): FixedNumber {
        const decimals = (_decimals == null) ? 0: getNumber(_decimals);
        const format = getFormat(_format);
 
        let value = getBigInt(_value, "value");
        const delta = decimals - format.decimals;
        if (delta > 0) {
            const tens = getTens(delta);
            assert((value % tens) === BN_0, "value loses precision for format", "NUMERIC_FAULT", {
                operation: "fromValue", fault: "underflow", value: _value
            });
            value /= tens;
        } else if (delta < 0) {
            value *= getTens(-delta);
        }
 
        checkValue(value, format, "fromValue");
 
        return new FixedNumber(_guard, value, format);
    }
 
    /**
     *  Creates a new [[FixedNumber]] for %%value%% with %%format%%.
     *
     *  This will throw a [[NumericFaultError]] if %%value%% cannot fit
     *  in %%format%%, either due to overflow or underflow (precision loss).
     */
    static fromString(_value: string, _format?: FixedFormat): FixedNumber {
        const match = _value.match(/^(-?)([0-9]*)\.?([0-9]*)$/);
        assertArgument(match && (match[2].length + match[3].length) > 0, "invalid FixedNumber string value", "value", _value);
 
        const format = getFormat(_format);
 
        let whole = (match[2] || "0"), decimal = (match[3] || "");
 
        // Pad out the decimals
        while (decimal.length < format.decimals) { decimal += Zeros; }
 
        // Check precision is safe
        assert(decimal.substring(format.decimals).match(/^0*$/), "too many decimals for format", "NUMERIC_FAULT", {
            operation: "fromString", fault: "underflow", value: _value
        });
 
        // Remove extra padding
        decimal = decimal.substring(0, format.decimals);
 
        const value = BigInt(match[1] + whole + decimal)
 
        checkValue(value, format, "fromString");
 
        return new FixedNumber(_guard, value, format);
    }
 
    /**
     *  Creates a new [[FixedNumber]] with the big-endian representation
     *  %%value%% with %%format%%.
     *
     *  This will throw a [[NumericFaultError]] if %%value%% cannot fit
     *  in %%format%% due to overflow.
     */
    static fromBytes(_value: BytesLike, _format?: FixedFormat): FixedNumber {
        let value = toBigInt(getBytes(_value, "value"));
        const format = getFormat(_format);

        if (format.signed) { value = fromTwos(value, format.width); }

        checkValue(value, format, "fromBytes");

        return new FixedNumber(_guard, value, format);
    }
}
 
//const f1 = FixedNumber.fromString("12.56", "fixed16x2");
//const f2 = FixedNumber.fromString("0.3", "fixed16x2");
//console.log(f1.divSignal(f2));
//const BUMP = FixedNumber.from("0.5");