@danielsimonjr/mathts-core 0.1.3 → 0.1.4

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/dist/index.d.ts CHANGED
@@ -734,6 +734,28 @@ declare class BigNumber implements MathTSValue {
734
734
  * Convert to bigint (truncated)
735
735
  */
736
736
  toBigInt(): bigint;
737
+ /**
738
+ * Binary string representation, e.g. `255 -> '0b11111111'`, `0.5 -> '0b0.1'`.
739
+ * Mirrors Decimal.js `toBinary()`. Negatives carry a leading `-`.
740
+ */
741
+ toBinary(): string;
742
+ /**
743
+ * Octal string representation, e.g. `8 -> '0o10'`.
744
+ * Mirrors Decimal.js `toOctal()`.
745
+ */
746
+ toOctal(): string;
747
+ /**
748
+ * Hexadecimal string representation, e.g. `255 -> '0xff'`, `10.5 -> '0xa.8'`.
749
+ * Mirrors Decimal.js `toHexadecimal()`.
750
+ */
751
+ toHexadecimal(): string;
752
+ /**
753
+ * Shared radix formatter for {@link toBinary}/{@link toOctal}/{@link toHexadecimal}.
754
+ * Integers and terminating fractions are exact; a non-terminating fraction is
755
+ * truncated at `maxFractionDigits` radix digits (a safety bound — the common
756
+ * use is integer formatting).
757
+ */
758
+ private toRadixString;
737
759
  /**
738
760
  * Addition
739
761
  */
package/dist/index.js CHANGED
@@ -1359,6 +1359,55 @@ var BigNumber = class _BigNumber {
1359
1359
  }
1360
1360
  return 0n;
1361
1361
  }
1362
+ /**
1363
+ * Binary string representation, e.g. `255 -> '0b11111111'`, `0.5 -> '0b0.1'`.
1364
+ * Mirrors Decimal.js `toBinary()`. Negatives carry a leading `-`.
1365
+ */
1366
+ toBinary() {
1367
+ return this.toRadixString(2, "0b");
1368
+ }
1369
+ /**
1370
+ * Octal string representation, e.g. `8 -> '0o10'`.
1371
+ * Mirrors Decimal.js `toOctal()`.
1372
+ */
1373
+ toOctal() {
1374
+ return this.toRadixString(8, "0o");
1375
+ }
1376
+ /**
1377
+ * Hexadecimal string representation, e.g. `255 -> '0xff'`, `10.5 -> '0xa.8'`.
1378
+ * Mirrors Decimal.js `toHexadecimal()`.
1379
+ */
1380
+ toHexadecimal() {
1381
+ return this.toRadixString(16, "0x");
1382
+ }
1383
+ /**
1384
+ * Shared radix formatter for {@link toBinary}/{@link toOctal}/{@link toHexadecimal}.
1385
+ * Integers and terminating fractions are exact; a non-terminating fraction is
1386
+ * truncated at `maxFractionDigits` radix digits (a safety bound — the common
1387
+ * use is integer formatting).
1388
+ */
1389
+ toRadixString(radix, prefix, maxFractionDigits = 1100) {
1390
+ if (this._isNaN) return "NaN";
1391
+ if (this._isInfinite) return (this._sign < 0 ? "-" : "") + "Infinity";
1392
+ if (this._sign === 0) return prefix + "0";
1393
+ const negative = this._sign < 0;
1394
+ const absVal = this.abs();
1395
+ const intPart = absVal.toBigInt();
1396
+ let out = prefix + intPart.toString(radix);
1397
+ let frac = absVal.subtract(_BigNumber.fromBigInt(intPart));
1398
+ if (!frac.isZero()) {
1399
+ const radixBN = _BigNumber.fromNumber(radix);
1400
+ let digits = "";
1401
+ for (let i = 0; i < maxFractionDigits && !frac.isZero(); i++) {
1402
+ frac = frac.multiply(radixBN);
1403
+ const digit = frac.toBigInt();
1404
+ digits += Number(digit).toString(radix);
1405
+ frac = frac.subtract(_BigNumber.fromBigInt(digit));
1406
+ }
1407
+ out += "." + digits;
1408
+ }
1409
+ return (negative ? "-" : "") + out;
1410
+ }
1362
1411
  // ============================================================
1363
1412
  // Arithmetic Operations
1364
1413
  // ============================================================
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danielsimonjr/mathts-core",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Core types, utilities, and configuration for MathTS",
5
5
  "author": "Daniel Simon Jr.",
6
6
  "license": "MIT",