compare()
compare()
compares two numbers. There is a method function and a static method function.
Syntax
method function
ba.compare(n);
static method function
BigArith.compare(a, b);
Parameters
method function
n - Required - {string|number|BigArith}
The number to compare with the value of the BigArith object. This could be a string of digits, a number, or a BigArith object.
static method function
a - Required - {string|number|BigArith}
The number to compare to. This could be a string of digits, a number, or a BigArith object.
b - Required - {string|number|BigArith}
The number to compare with. This could be a string of digits, a number, or a BigArith object.
Return value
method function - {integer}
-1
if value of BigArith object is less than n, 0
if value of BigArith object is equal to n, 1
if value of BigArith object is greater than n
static method function - {integer}
-1
if a is less than b, 0
if a is equal to b, 1
if a is greater than b.
Description
There are two functions which could be used, the method function, and the static method function. The method function takes one parameter (n) and returns an integer indicating whether value of object is lesser, equals or greater than n.
The static method function takes two parameters (a, b) and is always used as BigArith.compare()
. It returns an integer indicating whether a is lesser, equals or greater than b.
Any number parameter (that is not strings of digits or a BigArith) should be between the
Number.MIN_SAFE_INTEGER
andNumber.MAX_SAFE_INTEGER
limits.
-1
: BigArith object value < n or a < b.0
: BigArith object value == n or a == b.1
: BigArith object value > n or a > b.
Examples
In the server-side, always remember to add the line
var BigArith = require('bigarith.js');
however every other thing remains the same in both server-side and client-side code.
Using method function
var ba = new BigArith("-17031986");
console.log(ba.compare("24011985")); //logs -1
ba = new BigArith("+17031986");
console.log(ba.compare("24011985")); //logs -1
ba = new BigArith("+17031986");
console.log(ba.compare("17031986")); //logs 0
ba = new BigArith("+17031986");
console.log(ba.compare("99999999")); //logs 1
ba = new BigArith();
console.log(ba.compare(null)); //logs 0
ba = new BigArith("8888888888888888888888888888888888888888888888888888888");
console.log(ba.compare("99999999999999999999999999999999999999999999999999999999999999")); //logs -1
ba = new BigArith("23453434");
console.log(ba.compare(NaN)); //logs NaN
Using the static method function
console.log(BigArith.compare("-17031986", "24011985")); //logs -1
console.log(BigArith.compare("+17031986", "24011985")); //logs -1
console.log(BigArith.compare("8888888888888888888888888888888888888888888888888888888", "99999999999999999999999999999999999999999999999999999999999999")); //logs -1
console.log(BigArith.compare(null, "")); //logs 0
console.log(BigArith.compare("23453434", NaN)); //logs NaN
More examples here. Full documentation here