| 1669 | * frame. Outside a frame the result is `Math.random` (live). |
| 1670 | */ |
| 1671 | _substream(tag: number): RandomSubstream { |
| 1672 | return deriveSubstream(this._runtimeState.randomFrame, tag); |
| 1673 | } |
| 1674 | |
| 1675 | /** Replace a number that is close to 0 with the exact integer 0. |
| 1676 | * |
| 1677 | * How close to 0 the number has to be to be considered 0 is determined by {@linkcode tolerance}. |
| 1678 | */ |
| 1679 | chop(n: number): number; |
| 1680 | chop(n: BigDecimal): BigDecimal | 0; |
| 1681 | chop(n: Complex): Complex | 0; |
| 1682 | chop(n: number | BigDecimal | Complex): number | BigDecimal | Complex { |
| 1683 | const tolerance = this._numericConfiguration.tolerance; |
| 1684 | if (typeof n === 'number') { |
| 1685 | if (Math.abs(n) <= tolerance) return 0; |
| 1686 | return n; |
| 1687 | } |
| 1688 | |
| 1689 | if (n instanceof BigDecimal) { |
| 1690 | if (n.isPositive() && n.lte(this._numericConfiguration.bignumTolerance)) |
| 1691 | return 0; |
| 1692 | if ( |
| 1693 | n.isNegative() && |
| 1694 | n.gte(this._numericConfiguration.negBignumTolerance) |
| 1695 | ) |
| 1696 | return 0; |
| 1697 | if (n.isZero()) return 0; |
| 1698 | return n; |
| 1699 | } |
| 1700 | |
| 1701 | if ( |
| 1702 | n instanceof Complex && |
| 1703 | Math.abs(n.re) <= tolerance && |
| 1704 | Math.abs(n.im) <= tolerance |
| 1705 | ) |
| 1706 | return 0; |
| 1707 | |
| 1708 | return n; |
| 1709 | } |
| 1710 | |
| 1711 | /** Create an arbitrary precision number. |
| 1712 | * |
| 1713 | * The return value is an object with methods to perform arithmetic |
| 1714 | * operations: |
| 1715 | * - `toNumber()`: convert to a JavaScript `number` with potential loss of precision |
| 1716 | * - `add()` |
| 1717 | * - `sub()` |