Comprehensive comparison methods for BigInteger values including equality testing, ordering comparisons, absolute value comparisons, and min/max operations.
Perform a three-way comparison returning -1, 0, or 1.
/**
* Performs three-way comparison between two numbers
* @param number - Value to compare against
* @returns -1 if this < number, 0 if equal, 1 if this > number
*/
compare(number: BigNumber): number;
// Alias
compareTo(number: BigNumber): number;Usage Examples:
const bigInt = require('big-integer');
// Equal values
bigInt(5).compare(5); // 0
// First greater
bigInt(5).compare(4); // 1
// First lesser
bigInt(4).compare(5); // -1
// Large numbers
bigInt("999999999999").compare("1000000000000"); // -1
// Using for sorting
const numbers = [bigInt(5), bigInt(2), bigInt(8), bigInt(1)];
numbers.sort((a, b) => a.compare(b));
// Result: [1, 2, 5, 8]Compare the absolute values of two numbers.
/**
* Performs comparison between absolute values of two numbers
* @param number - Value to compare against
* @returns -1 if |this| < |number|, 0 if |this| = |number|, 1 if |this| > |number|
*/
compareAbs(number: BigNumber): number;Usage Examples:
// Compare absolute values
bigInt(5).compareAbs(-5); // 0 (|5| = |-5|)
bigInt(5).compareAbs(4); // 1 (|5| > |4|)
bigInt(4).compareAbs(-5); // -1 (|4| < |-5|)
// Useful for finding magnitude
bigInt(-100).compareAbs(50); // 1 (|-100| > |50|)Check if two BigInteger values are equal.
/**
* Checks if two numbers are equal
* @param number - Value to compare
* @returns True if numbers are equal
*/
equals(number: BigNumber): boolean;
// Alias
eq(number: BigNumber): boolean;Usage Examples:
// Equality check
bigInt(5).equals(5); // true
bigInt(4).equals(7); // false
// Compare with different types
bigInt(100).equals(100); // true (number)
bigInt(100).equals("100"); // true (string)
bigInt(100).equals(bigInt(100)); // true (BigInteger)
// Large number equality
bigInt("12345678901234567890").equals("12345678901234567890"); // trueCheck if two BigInteger values are not equal.
/**
* Checks if two numbers are not equal
* @param number - Value to compare
* @returns True if numbers are not equal
*/
notEquals(number: BigNumber): boolean;
// Alias
neq(number: BigNumber): boolean;Usage Examples:
// Inequality check
bigInt(5).notEquals(5); // false
bigInt(4).notEquals(7); // true
// Using in conditionals
const value = bigInt(42);
if (value.notEquals(0)) {
console.log("Non-zero value");
}Check if the first number is strictly greater than the second.
/**
* Checks if the first number is greater than the second
* @param number - Value to compare against
* @returns True if this > number
*/
greater(number: BigNumber): boolean;
// Alias
gt(number: BigNumber): boolean;Usage Examples:
// Greater than check
bigInt(5).greater(4); // true
bigInt(5).greater(5); // false
bigInt(5).greater(6); // false
// Negative numbers
bigInt(-5).greater(-10); // true
bigInt(-5).greater(0); // falseCheck if the first number is greater than or equal to the second.
/**
* Checks if the first number is greater than or equal to the second
* @param number - Value to compare against
* @returns True if this >= number
*/
greaterOrEquals(number: BigNumber): boolean;
// Alias
geq(number: BigNumber): boolean;Usage Examples:
// Greater than or equal check
bigInt(5).greaterOrEquals(4); // true
bigInt(5).greaterOrEquals(5); // true
bigInt(5).greaterOrEquals(6); // false
// Range validation
const value = bigInt(100);
const min = bigInt(50);
if (value.greaterOrEquals(min)) {
console.log("Value is within range");
}Check if the first number is strictly less than the second.
/**
* Checks if the first number is less than the second
* @param number - Value to compare against
* @returns True if this < number
*/
lesser(number: BigNumber): boolean;
// Alias
lt(number: BigNumber): boolean;Usage Examples:
// Less than check
bigInt(5).lesser(6); // true
bigInt(5).lesser(5); // false
bigInt(5).lesser(4); // false
// Negative numbers
bigInt(-10).lesser(-5); // true
bigInt(0).lesser(-5); // falseCheck if the first number is less than or equal to the second.
/**
* Checks if the first number is less than or equal to the second
* @param number - Value to compare against
* @returns True if this <= number
*/
lesserOrEquals(number: BigNumber): boolean;
// Alias
leq(number: BigNumber): boolean;Usage Examples:
// Less than or equal check
bigInt(5).lesserOrEquals(6); // true
bigInt(5).lesserOrEquals(5); // true
bigInt(5).lesserOrEquals(4); // false
// Range validation
const value = bigInt(100);
const max = bigInt(200);
if (value.lesserOrEquals(max)) {
console.log("Value is within range");
}Find the larger of two BigInteger values.
/**
* Returns the larger of two values
* @param a - First value
* @param b - Second value
* @returns The larger BigInteger
*/
bigInt.max(a: BigNumber, b: BigNumber): BigInteger;Usage Examples:
// Find maximum
bigInt.max(77, 432); // 432
// With negative numbers
bigInt.max(-10, -5); // -5
// Large numbers
bigInt.max("999999999999", "1000000000000"); // 1000000000000
// Find max in array
const numbers = [bigInt(5), bigInt(2), bigInt(9), bigInt(1)];
const maximum = numbers.reduce((max, num) => bigInt.max(max, num));
// maximum = 9Find the smaller of two BigInteger values.
/**
* Returns the smaller of two values
* @param a - First value
* @param b - Second value
* @returns The smaller BigInteger
*/
bigInt.min(a: BigNumber, b: BigNumber): BigInteger;Usage Examples:
// Find minimum
bigInt.min(77, 432); // 77
// With negative numbers
bigInt.min(-10, -5); // -10
// Large numbers
bigInt.min("999999999999", "1000000000000"); // 999999999999
// Find min in array
const numbers = [bigInt(5), bigInt(2), bigInt(9), bigInt(1)];
const minimum = numbers.reduce((min, num) => bigInt.min(min, num));
// minimum = 1/**
* Check if value is within a range [min, max]
*/
function isInRange(value, min, max) {
return value.greaterOrEquals(min) && value.lesserOrEquals(max);
}
const value = bigInt(50);
isInRange(value, bigInt(0), bigInt(100)); // true
isInRange(value, bigInt(60), bigInt(100)); // false/**
* Clamp value to range [min, max]
*/
function clamp(value, min, max) {
if (value.lesser(min)) return min;
if (value.greater(max)) return max;
return value;
}
clamp(bigInt(150), bigInt(0), bigInt(100)); // 100
clamp(bigInt(-10), bigInt(0), bigInt(100)); // 0
clamp(bigInt(50), bigInt(0), bigInt(100)); // 50// Ascending sort
const numbers = [bigInt(5), bigInt(2), bigInt(9), bigInt(1)];
numbers.sort((a, b) => a.compare(b));
// [1, 2, 5, 9]
// Descending sort
numbers.sort((a, b) => b.compare(a));
// [9, 5, 2, 1]
// Sort by absolute value
numbers.sort((a, b) => a.compareAbs(b));const numbers = [
bigInt("123456789012345"),
bigInt("987654321098765"),
bigInt("555555555555555")
];
// Find maximum
const max = numbers.reduce((acc, num) => bigInt.max(acc, num));
// 987654321098765
// Find minimum
const min = numbers.reduce((acc, num) => bigInt.min(acc, num));
// 123456789012345
// Find maximum absolute value
const maxAbs = numbers.reduce((acc, num) =>
acc.compareAbs(num) > 0 ? acc : num
);const balance = bigInt("1000000000000");
const threshold = bigInt("500000000000");
if (balance.greater(threshold)) {
console.log("Balance exceeds threshold");
}
// Multiple conditions
const value = bigInt(75);
const min = bigInt(50);
const max = bigInt(100);
if (value.greaterOrEquals(min) && value.lesserOrEquals(max)) {
console.log("Value is in valid range");
}/**
* Determine relationship between signs
*/
function sameSign(a, b) {
return (a.isPositive() && b.isPositive()) ||
(a.isNegative() && b.isNegative()) ||
(a.isZero() && b.isZero());
}
sameSign(bigInt(5), bigInt(10)); // true
sameSign(bigInt(-5), bigInt(-10)); // true
sameSign(bigInt(5), bigInt(-10)); // falseFor convenience, comparison methods have shorter aliases:
compare() ↔ compareTo()equals() ↔ eq()notEquals() ↔ neq()greater() ↔ gt()greaterOrEquals() ↔ geq()lesser() ↔ lt()lesserOrEquals() ↔ leq()All comparison operations are O(n) where n is the number of digits, as they may need to compare each digit. However, early termination is used when possible (e.g., different signs, different lengths).