or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

arithmetic.mdcomparison.mdconfiguration.mdconstructor.mdindex.mdprecision.mdstring-conversion.mdtype-checking.md
tile.json

type-checking.mddocs/

Type Checking and Properties

Methods for checking number types, properties, and utility operations for BigNumber instances.

Capabilities

Finite Number Check

Tests if the BigNumber represents a finite number.

/**
 * Tests if this BigNumber is finite
 * @returns {boolean} True if finite, false if NaN or ±Infinity
 */
isFinite(): boolean

Usage Examples:

const finite = new BigNumber(123.456);
const infinite = new BigNumber(Infinity);
const nan = new BigNumber(NaN);

finite.isFinite();    // true
infinite.isFinite();  // false
nan.isFinite();       // false

// Edge cases
new BigNumber(0).isFinite();           // true
new BigNumber('1e308').isFinite();     // true (within BigNumber range)
new BigNumber('1e1000000').isFinite(); // false (exceeds range, becomes Infinity)

Integer Check

Tests if the BigNumber represents an integer (whole number).

/**
 * Tests if this BigNumber is an integer
 * @returns {boolean} True if integer, false if decimal or special value
 */
isInteger(): boolean

Usage Examples:

const integer = new BigNumber(42);
const decimal = new BigNumber(42.5);
const zero = new BigNumber(0);

integer.isInteger();   // true
decimal.isInteger();   // false
zero.isInteger();      // true

// Large integers
new BigNumber('123456789012345678901234567890').isInteger();  // true

// Edge cases
new BigNumber(Infinity).isInteger();   // false
new BigNumber(NaN).isInteger();        // false
new BigNumber('42.0').isInteger();     // true (no fractional part)

NaN Check

Tests if the BigNumber represents NaN (Not a Number).

/**
 * Tests if this BigNumber is NaN
 * @returns {boolean} True if NaN, false otherwise
 */
isNaN(): boolean

Usage Examples:

const nan = new BigNumber(NaN);
const valid = new BigNumber(123);
const invalid = new BigNumber('invalid');

nan.isNaN();      // true
valid.isNaN();    // false
invalid.isNaN();  // true (invalid input becomes NaN)

// Operations that produce NaN
new BigNumber(0).dividedBy(0).isNaN();           // true
new BigNumber(-1).squareRoot().isNaN();          // true
new BigNumber(Infinity).minus(Infinity).isNaN(); // true

Negative Check

Tests if the BigNumber is negative (less than zero).

/**
 * Tests if this BigNumber is negative
 * @returns {boolean} True if negative, false if positive, zero, or NaN
 */
isNegative(): boolean

Usage Examples:

const positive = new BigNumber(5);
const negative = new BigNumber(-5);
const zero = new BigNumber(0);
const negZero = new BigNumber(-0);

positive.isNegative();  // false
negative.isNegative();  // true
zero.isNegative();      // false
negZero.isNegative();   // true (distinguishes -0 from 0)

// Special values
new BigNumber(Infinity).isNegative();   // false
new BigNumber(-Infinity).isNegative();  // true
new BigNumber(NaN).isNegative();        // false

Positive Check

Tests if the BigNumber is positive (greater than zero).

/**
 * Tests if this BigNumber is positive
 * @returns {boolean} True if positive, false if negative, zero, or NaN
 */
isPositive(): boolean

Usage Examples:

const positive = new BigNumber(5);
const negative = new BigNumber(-5);
const zero = new BigNumber(0);
const negZero = new BigNumber(-0);

positive.isPositive();  // true
negative.isPositive();  // false
zero.isPositive();      // false
negZero.isPositive();   // false

// Special values
new BigNumber(Infinity).isPositive();    // true
new BigNumber(-Infinity).isPositive();   // false
new BigNumber(NaN).isPositive();         // false

// Very small positive numbers
new BigNumber('1e-1000').isPositive();   // true

Zero Check

Tests if the BigNumber is zero (positive or negative zero).

/**
 * Tests if this BigNumber is zero
 * @returns {boolean} True if zero or negative zero, false otherwise
 */
isZero(): boolean

Usage Examples:

const zero = new BigNumber(0);
const negZero = new BigNumber(-0);
const tiny = new BigNumber('1e-1000');
const negative = new BigNumber(-5);

zero.isZero();     // true
negZero.isZero();  // true (both +0 and -0 are zero)
tiny.isZero();     // false (very small but not zero)
negative.isZero(); // false

// Operations resulting in zero
new BigNumber(5).minus(5).isZero();        // true
new BigNumber('1e-2000').times(0).isZero(); // true

// Special values
new BigNumber(NaN).isZero();      // false
new BigNumber(Infinity).isZero(); // false

Utility Methods

Absolute Value

Returns the absolute value (magnitude) of the BigNumber.

/**
 * Returns absolute value of this BigNumber
 * @returns {BigNumber} Absolute value (always non-negative)
 */
absoluteValue(): BigNumber

/**
 * Alias for absoluteValue
 */
abs(): BigNumber

Usage Examples:

const positive = new BigNumber(5);
const negative = new BigNumber(-5);
const zero = new BigNumber(-0);

positive.absoluteValue();  // '5'
negative.abs();            // '5'
zero.abs();               // '0' (removes negative sign)

// Large numbers
new BigNumber('-123456789012345678901234567890').abs();
// '123456789012345678901234567890'

// Special values
new BigNumber(-Infinity).abs();  // 'Infinity'
new BigNumber(NaN).abs();        // 'NaN'

Negation

Returns the negated value of the BigNumber.

/**
 * Returns negated value of this BigNumber
 * @returns {BigNumber} Value with opposite sign
 */
negated(): BigNumber

Usage Examples:

const positive = new BigNumber(5);
const negative = new BigNumber(-5);
const zero = new BigNumber(0);

positive.negated();  // '-5'
negative.negated();  // '5'
zero.negated();      // '0' (zero negation is zero)

// Chaining operations
const x = new BigNumber(10);
const result = x.negated().plus(20);  // '10' ((-10) + 20)

// Special values
new BigNumber(Infinity).negated();   // '-Infinity'
new BigNumber(-Infinity).negated();  // 'Infinity'
new BigNumber(NaN).negated();        // 'NaN'

Type Checking Patterns

Comprehensive Value Validation

function analyzeNumber(bn) {
  const analysis = {
    isFinite: bn.isFinite(),
    isInteger: bn.isInteger(),
    isNaN: bn.isNaN(),
    isNegative: bn.isNegative(),
    isPositive: bn.isPositive(),
    isZero: bn.isZero()
  };
  
  if (analysis.isNaN) return "Invalid number";
  if (analysis.isZero) return "Zero";
  if (!analysis.isFinite) return analysis.isNegative ? "Negative Infinity" : "Positive Infinity";
  
  let type = analysis.isInteger ? "Integer" : "Decimal";
  let sign = analysis.isNegative ? "Negative" : "Positive";
  
  return `${sign} ${type}`;
}

// Usage examples
analyzeNumber(new BigNumber(42));      // "Positive Integer"
analyzeNumber(new BigNumber(-3.14));   // "Negative Decimal"
analyzeNumber(new BigNumber(0));       // "Zero"
analyzeNumber(new BigNumber(NaN));     // "Invalid number"

Safe Operations Based on Type

function safeOperation(bn) {
  if (bn.isNaN()) {
    throw new Error("Cannot operate on NaN");
  }
  
  if (!bn.isFinite()) {
    throw new Error("Cannot operate on infinite values");
  }
  
  if (bn.isNegative() && needsPositive) {
    bn = bn.abs();  // Make positive if needed
  }
  
  return bn.squareRoot();  // Safe to calculate square root
}

Performance Notes

  • Type checking methods are optimized and fast
  • Multiple checks can be combined efficiently
  • Special value detection is handled at the native level
  • Methods return boolean values immediately without calculation