or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

arithmetic.mdbitwise.mdcomparison.mdconversion.mdindex.mdreduction.md
tile.json

comparison.mddocs/

Comparison and Boolean Tests

Methods for comparing BN instances with each other and with JavaScript numbers, plus boolean tests for number properties.

Capabilities

Comparison Operations

Compare BN instances using standard comparison operators with support for both BN and JavaScript number comparisons.

/**
 * Compare this number with another BN
 * @param {BN} num - Number to compare with
 * @returns {number} -1 if this < num, 0 if equal, 1 if this > num
 */
cmp(num: BN): number;

/**
 * Compare this number with a JavaScript number
 * @param {number} num - JavaScript number to compare with
 * @returns {number} -1 if this < num, 0 if equal, 1 if this > num
 */
cmpn(num: number): number;

/**
 * Unsigned comparison (ignoring signs)
 * @param {BN} num - Number to compare with
 * @returns {number} -1 if |this| < |num|, 0 if equal, 1 if |this| > |num|
 */
ucmp(num: BN): number;

Usage Examples:

const BN = require('bn.js');

const a = new BN('100');
const b = new BN('50');
const c = new BN('-25');

// Basic comparison
console.log(a.cmp(b));   // 1 (100 > 50)
console.log(b.cmp(a));   // -1 (50 < 100)
console.log(a.cmp(a));   // 0 (100 == 100)

// Compare with JavaScript number
console.log(a.cmpn(75)); // 1 (100 > 75)
console.log(b.cmpn(50)); // 0 (50 == 50)

// Unsigned comparison
console.log(b.ucmp(c));  // 1 (|50| > |-25|)

Equality Tests

Test for equality between BN instances and JavaScript numbers.

/**
 * Test equality with another BN
 * @param {BN} num - Number to compare with
 * @returns {boolean} True if numbers are equal
 */
eq(num: BN): boolean;

/**
 * Test equality with a JavaScript number
 * @param {number} num - JavaScript number to compare with
 * @returns {boolean} True if numbers are equal
 */
eqn(num: number): boolean;

Greater Than Comparisons

Test if this number is greater than another number.

/**
 * Test if this number is greater than another BN
 * @param {BN} num - Number to compare with
 * @returns {boolean} True if this > num
 */
gt(num: BN): boolean;

/**
 * Test if this number is greater than a JavaScript number
 * @param {number} num - JavaScript number to compare with
 * @returns {boolean} True if this > num
 */
gtn(num: number): boolean;

/**
 * Test if this number is greater than or equal to another BN
 * @param {BN} num - Number to compare with
 * @returns {boolean} True if this >= num
 */
gte(num: BN): boolean;

/**
 * Test if this number is greater than or equal to a JavaScript number
 * @param {number} num - JavaScript number to compare with
 * @returns {boolean} True if this >= num
 */
gten(num: number): boolean;

Less Than Comparisons

Test if this number is less than another number.

/**
 * Test if this number is less than another BN
 * @param {BN} num - Number to compare with
 * @returns {boolean} True if this < num
 */
lt(num: BN): boolean;

/**
 * Test if this number is less than a JavaScript number
 * @param {number} num - JavaScript number to compare with
 * @returns {boolean} True if this < num
 */
ltn(num: number): boolean;

/**
 * Test if this number is less than or equal to another BN
 * @param {BN} num - Number to compare with
 * @returns {boolean} True if this <= num
 */
lte(num: BN): boolean;

/**
 * Test if this number is less than or equal to a JavaScript number
 * @param {number} num - JavaScript number to compare with
 * @returns {boolean} True if this <= num
 */
lten(num: number): boolean;

Usage Examples:

const BN = require('bn.js');

const a = new BN('100');
const b = new BN('50');
const c = new BN('100');

// Equality tests
console.log(a.eq(c));    // true
console.log(a.eq(b));    // false
console.log(a.eqn(100)); // true

// Greater than tests
console.log(a.gt(b));    // true (100 > 50)
console.log(b.gt(a));    // false (50 < 100)
console.log(a.gte(c));   // true (100 >= 100)
console.log(a.gtn(75));  // true (100 > 75)

// Less than tests
console.log(b.lt(a));    // true (50 < 100)
console.log(a.lt(b));    // false (100 > 50)
console.log(a.lte(c));   // true (100 <= 100)
console.log(b.ltn(75));  // true (50 < 75)

Boolean Property Tests

Test various properties of BN instances.

/**
 * Test if the number is zero
 * @returns {boolean} True if the number equals zero
 */
isZero(): boolean;

/**
 * Test if the number is negative
 * @returns {boolean} True if the number is less than zero
 */
isNeg(): boolean;

/**
 * Test if the number is even
 * @returns {boolean} True if the number is divisible by 2
 */
isEven(): boolean;

/**
 * Test if the number is odd
 * @returns {boolean} True if the number is not divisible by 2
 */
isOdd(): boolean;

Usage Examples:

const BN = require('bn.js');

const zero = new BN('0');
const positive = new BN('42');
const negative = new BN('-17');
const even = new BN('100');
const odd = new BN('101');

// Zero test
console.log(zero.isZero());     // true
console.log(positive.isZero()); // false

// Sign test
console.log(negative.isNeg());  // true
console.log(positive.isNeg());  // false
console.log(zero.isNeg());      // false

// Even/odd tests
console.log(even.isEven());     // true
console.log(odd.isEven());      // false
console.log(even.isOdd());      // false
console.log(odd.isOdd());       // true

Utility Functions

Additional utility functions for working with comparisons.

/**
 * Utility function to get the maximum of two BN numbers
 * @param {BN} left - First number
 * @param {BN} right - Second number  
 * @returns {BN} The larger of the two numbers
 */
BN.max(left: BN, right: BN): BN;

/**
 * Utility function to get the minimum of two BN numbers
 * @param {BN} left - First number
 * @param {BN} right - Second number
 * @returns {BN} The smaller of the two numbers
 */
BN.min(left: BN, right: BN): BN;

Usage Examples:

const BN = require('bn.js');

const a = new BN('100');
const b = new BN('200');

const maximum = BN.max(a, b);
const minimum = BN.min(a, b);

console.log(maximum.toString()); // "200"
console.log(minimum.toString()); // "100"

// Useful for bounds checking
const value = new BN('150');
const lowerBound = new BN('100');
const upperBound = new BN('200');

const isInRange = value.gte(lowerBound) && value.lte(upperBound);
console.log(isInRange); // true

Common Patterns

Range Checking

const BN = require('bn.js');

function isInRange(value, min, max) {
  return value.gte(min) && value.lte(max);
}

const value = new BN('150');
const min = new BN('100');
const max = new BN('200');

console.log(isInRange(value, min, max)); // true

Finding Maximum in Array

const BN = require('bn.js');

function findMax(bnArray) {
  if (bnArray.length === 0) return null;
  
  return bnArray.reduce((max, current) => 
    current.gt(max) ? current : max
  );
}

const numbers = [
  new BN('100'),
  new BN('250'), 
  new BN('175')
];

const maximum = findMax(numbers);
console.log(maximum.toString()); // "250"

Sorting BN Arrays

const BN = require('bn.js');

const numbers = [
  new BN('300'),
  new BN('100'),
  new BN('200')
];

// Sort ascending
numbers.sort((a, b) => a.cmp(b));
console.log(numbers.map(n => n.toString())); // ["100", "200", "300"]

// Sort descending  
numbers.sort((a, b) => b.cmp(a));
console.log(numbers.map(n => n.toString())); // ["300", "200", "100"]