-
-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Let´s start creating our first BaseNumber. The constructor uses two arguments (last optional) to build the instance:
Argument | Type | Detail |
---|---|---|
number |
BaseNumber / String / Number / BigInt | Required |
base |
Number | Optional |
const dec = Base(10, 10); // Base(number, base);
const hex = Base("f2", 16);
BaseNumber.js works with decimal base numbers by default, so you can omit the base argument:
const dec = Base(10);
The library allows user to create signed numbers. Specify the sign in the first argument of the instance:
const dec = Base(-10);
const oct = Base("--++10", 8); // it´s "10" in base 8
const hex = Base("-+10", 2); // it´s "-10" in base 2
BaseNumber.js also allows scientific notation within your numbers. You can init an instance this way:
const dec = Base(10e-2); // equals to "0.1" in base 10
const oct = Base(10e+2, 8); // equals to "1000" in base 8
const hex = Base("ff.e10e-2", 16); // equals to "0.ffe10" in base 16
Notice that e-
/ e+
is referred to scientific notation. Omitting the sign (+
or -
) may cause errors or unexpected results:
const hex1 = Base("ff.e10e-2", 16); // equals to "0.ffe10" in base 16
const hex2 = Base("ff.e10e2", 16); // equals to "ff.e10e2" in base 16, no scientific notation here
const dec = Base("10e2"); // Error, number doesn´t match base
Returns a String that may contain letters and numbers, where 0
is a signed number :
x = Base(-0);
y = Base(0);
z = Base("3a0", 16);
x.valueOf(); // "-0"
y.valueOf(); // "0"
z.valueOf(); // "3a0"
Base(Infinity).valueOf() // "Infinity"
Base(NaN).valueOf() // "NaN"
Same as valueOf()
but 0 is not signed:
x = Base(-0);
y = Base(0);
x.toString(); // "0"
y.toString(); // "0"
Returns the number representation of the instance. If the instance is in base different than 10, the method would return the decimal representation of the instance
dec = Base(10);
hex = Base("a", 16);
pi = Base("3.1415926535897932384626433832795028841");
dec.toNumber(); // returns 10
hex.toNumber(); // returns 10
pi.toNumber(); // returns 3.141592653589793
Number base is saved as an integer:
dec = Base(10);
hex = Base("f2", 16);
dec.base(); // 10
hex.base(); // 16
Returns a value representing the sign of the instance:
Sign | Return |
---|---|
+ |
1 |
- |
-1 |
+0 |
0 |
-0 |
-0 |
NaN |
NaN |
x = Base("-abc", 16)
y = Base("NaN")
z = Base("le5k1p", 36)
x.sign() // -1
y.sign() // NaN
z.sign() // 1
Base(0).sign() // 0
Base(-0).sign() // -0
The newValue
method allows user to modify the value of an instance. It takes two parameters, second is optional, and returns the same BaseNumber instance modified:
Argument | Type | Detail |
---|---|---|
number |
BaseNumber / String / Number / BigInt | Required |
base |
Number | Optional |
dec.newValue(number[, base]);
The number
argument can be either a String/Number or another BaseNumber instance. In case it´s a BaseNumber instance, base
parameter would be equal to the argument's base
. E.g:
dec.newValue(hex); // base argument is by default hex.base()
// Now 'dec' is a copy of 'hex' instance (same value and same base)
In case number argument is a string/float variable, the base
parameter would be equal to the instance base by default, unless you specify it:
dec.newValue(5); // base argument is by default dec.base()
// New value is 5 in base 10
This method returns the updated instance:
const dec = Base(10);
dec.newValue(5).valueOf(); // returns "5" in base 10
dec.valueOf() // returns "5" in base 10
BaseNumber.js has its own method to parse a number into a new base. It returns a new BaseNumber:
dec = Base(15);
dec.toBase(16).valueOf(); // returns "f" (15 in hexadecimal)
If base parameter is omitted, number would be parsed in base 10 by default.
Similar to toBase()
, returns a value parsed in decimal base. It does not modify the instance itself. It´s a way to improve code readlibility:
hex = Base("f", 16);
hex.toDec().valueOf(); // returns "15"
dec = Base(15);
dec.toHex().valueOf(); // returns "f"
dec = Base(10);
dec.toOct().valueOf(); // returns "12"
dec = Base(10.0625);
dec.toBin().valueOf(); // returns "1010.0001"
Returns a new BaseNumber only with the integer part, similar to Javascript native parseInt()
dec = Base(10.0625);
dec.trunc().valueOf(); // returns "10"
It rounds a number according to the precision passed as an argument, rounding up or down depending the number (and user parameters). The method returns a new instance with decimals rounded:
Argument | Type | Detail |
---|---|---|
precision |
Number | Required |
exclusive |
Boolean | Optional |
dec.round(precision[, exclusive]);
precision
argument must be higher than 0 to avoid errors. If precision
arg. is omitted, it would be 1 by default. exclusive
argument allows user to decide how to round the middle number between 0 and the base. E.g: suppose a base 9 number:
The 4 digit is the middle number because base is an odd number (9), and the allowed symbols in the base are [0, 1, 2, 3, (4), 5, 6, 7, 8]
.
x = Base(10.04, 9);
// by default, exclusive is false, so the middle number would be round up
x.round(1).valueOf(); // returns "10.1"
// exclusive is true, so the middle number would be round down
x.round(1, true).valueOf(); // returns "10"
Argument | Type | Detail |
---|---|---|
precision |
Number | Required |
exclusive |
Boolean | Optional |
Returns a String containing the number with precision
decimals.
dec.toFixed(precision[, exclusive]);
precision
argument must be higher or equal to 0 to avoid errors. If precision
arg. is omitted, it would returns the same as valueOf().
exclusive
argument allows user to decide how to round the middle number between 0 and the base, see round() for more info about this parameter.
x = Base("4.5394")
x.toFixed(); // '4.5394'
x.toFixed(10); // '4.5394000000'
x.toFixed(2); // '4.54'
Argument | Type | Detail |
---|---|---|
precision |
Number | Required |
exclusive |
Boolean | Optional |
Returns a String containing the number rounded with precision
digits.
dec.toPrecision(precision[, exclusive]);
precision
argument must be higher than 0 to avoid errors. If precision
arg. is omitted, it would returns the same as valueOf(). If precision
is less than the number of digits necessary to represent the integer part of the value in normal notation, then exponential notation is used.
exclusive
argument allows user to decide how to round the middle number between 0 and the base, see round() for more info about this parameter.
x = Base("4765456.5394")
x.toPrecision(); // '4765456.5394'
x.toPrecision(15); // '4765456.53940000'
x.toPrecision(2); // '4.8 e+6'
Argument | Type | Detail |
---|---|---|
precision |
Number | Required |
exclusive |
Boolean | Optional |
Returns a new BaseNumber whose value is the value of this instance rounded to precision
significant digits.
dec.toSignificantDigits(precision[, exclusive]);
precision
argument must be higher than 0 to avoid errors. If precision
arg. is omitted, it would returns the same as valueOf().
exclusive
argument allows user to decide how to round the middle number between 0 and the base, see round() for more info about this parameter.
dec = Base(9876.54321)
dec.toSignificantDigits() // '9876.54321'
dec.toSignificantDigits(6) // '9876.54'
dec.toSD(2) // '9900'
Returns a new BaseNumber whose value is the absolute value the instance.
dec = Base(-99);
dec.abs().valueOf() // '99'
Returns a new BaseNumber whose value is the value of the instance rounded to a whole number in the direction of positive Infinity.
x = Base(1.3)
x.ceil().valueOf() // '2'
y = Base(-1.8)
y.ceil().valueOf() // '-1'
z = Base("7a.2", 16)
z.ceil().valueOf() // '7b'
Returns a new BaseNumber whose value is the value of the instance rounded to a whole number in the direction of negative Infinity.
x = Base(1.3)
x.ceil().valueOf() // '1'
y = Base(-1.8)
y.ceil().valueOf() // '-2'
z = Base("7a.2", 16)
z.ceil().valueOf() // '79'
Argument | Type | Detail |
---|---|---|
min |
BaseNumber | Required |
max |
BaseNumber | Optional |
Returns a new BaseNumber whose value is the value of this instance clamped to the range delineated by min and max.
x = Base(5)
min = Base(100)
max = Base(150)
x.clamp(min, max).valueOf() // '100'
Argument | Type | Detail |
---|---|---|
precision |
Number | Optional |
Returns a string representing the value of the instance rounded in exponential notation. Specify the number of digit after comma as an argument precision
. By default precision
is equal to the number of digits that are necessary to represent the number accurately.
dec = Base(9876.54321)
hex = Base("0.0f4ee21", 16)
dec.toExp() // '9.87654321 e+3'
dec.toExp(3) // '9.876 e+3'
dec.toExp(15) // '9.876543210000000 e+3'
hex.toExp() // 'f.4ee21 e-2'
hex.toExp(2) // 'f.4e e-2'
Returns a new BaseNumber with the opposite value of the instance:
x = Base(10)
y = Base("-32", 16)
x.neg().valueOf() // '-10'
y.neg().valueOf() // '32'
BaseNumber.js includes a method in which a number in any base can be transfromed to IEEE754 representation. Call the toIEEE754()
method, it returns an object with three keys: exponent
, mantissa
and sign
:
dec.toIEEE754([bits64]);
The bits64
argument allows to change the 32 bit floating point representation into a 64 bit representation. It´s by default false
(32 bits).
const dec = Base(10.625);
dec.toIEEE754();
/* returns
{
exponent: "10000010",
mantissa: "01010100000000000000000",
sign: "0"
} */
BaseNumber allows you to make math operations with normal variables or another BaseNumber instance. Although following examples show only integer numbers, all math operation are also available for float numbers.:
The addition method takes two arguments, last optional:
Argument | Type | Detail |
---|---|---|
number |
BaseNumber / String / Number / BigInt | Required |
base |
Number | Optional |
dec.add(number[, base]);
The number
argument can be either a string/number variable or another BaseNumber instance. In case it´s a BaseNumber instance, base
parameter would be equal to the number instance's base
. E.g:
dec = Base(10);
hex = Base("ff", 16);
dec.add(hex); // base argument is by default hex.base()
dec.add(hex, 10); // base argument is by default hex.base(), doesn´t matter if there is a base argument
In case number argument is a string/float variable, the base
parameter would be 10 by default, unless you specify it:
dec.add(77); // Base by default is 10
dec.add(77, 8); // Base is 8
Since the return value of the math functions is the modified instance, in order to take the value you need to add the valueOf() method at the end of the operation.
dec.add(77).valueOf() // returns the new value
The remaining Math operations works exactly the same:
dec = Base(10);
dec.subtract(5).valueOf(); // "5"
dec.subtract("f", 16).valueOf(); // "-5"
dec = Base(10);
bin = Base(1010, 2)
dec.multiply(5).valueOf(); // "50"
dec.multiply(bin).valueOf(); // "100"
dec = Base(10);
dec.divide(5).valueOf(); // "2"
dec.divide("a", 16).valueOf(); // "1"
dec = Base(10);
dec.pow(2).valueOf(); // "100"
dec.pow(10, 2).valueOf(); // "100"
dec = Base(10);
dec.root(2).valueOf(); // "3.16227766...."
dec.root(2.4).valueOf(); // "2.61015721...."
dec.root("10", 2).valueOf(); // "3.16227766...."
The same as root(2)
:
dec = Base(10);
dec.sqrt().valueOf(); // "3.16227766...."
The same as root(3)
:
dec = Base(125);
dec.cbrt().valueOf(); // "5"
Returns a new BaseNumber whose value is the base e
(Euler's number, the base of the natural logarithm) exponential of the instance
x = Base(1)
x.exp().valueOf() // '2.7182818284590452354'
Returns a new BaseNumber whose value is the base x logarithm of the instance. If x is omitted, the base 10 logarithm of the instance will be returned.
x = Base(8)
x.log().valueOf(); // '0.903089....'
y = Base("100", 16);
y.log(2).valueOf(); // '8' in base 16
y.log("10", 2).valueOf(); // '8' in base 16
Returns a new BaseNumber whose value is the natural logarithm of the instance.
The ln() is the inverse of the exp()
function.
x = Base(1)
x.ln().valueOf(); // '0'
y = Base("7a.2", 16);
y.ln().valueOf(); // '4.ce176fb4d0...'
Returns a new BaseNumber whose value is the cosine of the instance.
The operation would depend of the angle unit, see setAngle()
.
x = Base(90)
x.cos().valueOf(); // '0'
y = Base(0);
y.cos().valueOf(); // '1'
Returns a new BaseNumber whose value is the sine of the instance.
The operation would depend of the angle unit, see setAngle()
.
x = Base(90)
x.sin().valueOf(); // '1'
y = Base(0);
y.sin().valueOf(); // '0'
Returns a new BaseNumber whose value is the tangent of the instance.
The operation would depend of the angle unit, see setAngle()
.
x = Base(90)
x.tan().valueOf(); // 'Infinity'
y = Base(0);
y.tan().valueOf(); // '0'
Returns a new BaseNumber whose value is the inverse cosine of the instance.
The argument range is [-1, 1], else returns NaN
.
The result would depend of the angle unit, see setAngle()
.
x = Base(1)
x.acos().valueOf(); // '0'
y = Base(0);
y.acos().valueOf(); // '90'
Returns a new BaseNumber whose value is the inverse sine of the instance.
The argument range is [-1, 1], else returns NaN
.
The result would depend of the angle unit, see setAngle()
.
x = Base(1)
x.asin().valueOf(); // '90'
y = Base(0);
y.asin().valueOf(); // '0'
Returns a new BaseNumber whose value is the inverse tangent of the instance.
The argument range is [-Infinity, Infinity].
The result would depend of the angle unit, see setAngle()
.
x = Base(1)
x.atan().valueOf(); // '45'
y = Base(0);
y.atan().valueOf(); // '0'
Returns a new BaseNumber whose value is the factorial value of the instance. If the number is a float, it would be truncated to ist integer part
x = Base(3)
x.fact().valueOf(); // '6'
y = Base(4.5345);
y.fact().valueOf(); // '24'
BaseNumber.js allows user to chain math and other operators that return a BaseNumber. The list of operators that can be chained is the following:
newValue()
round()
trunc()
clamp()
abs()
neg()
floor()
ceil()
toBase()
toDec()
toHex()
toBin()
toOct()
add()
subtract()
multiply()
divide()
pow()
root()
sqrt()
cbrt()
exp()
log()
ln()
cos()
sin()
tan()
acos()
asin()
atan()
fact()
clone()
E.g:
const dec = Base(10);
dec.add(5).subtract("f", 16).subtract(1).pow(2).toBin().toString(); // returns "1"
Returns a new BaseNumber instance, which is a copy of the original isntance:
const dec = Base(10);
const copy = dec.clone();
copy.toString() // '10'
BaseNumber.js allows user to make comparisons between instances or variables. Although following examples show only integer numbers, all comparing operations are also available for float numbers:
Call the equalTo()
method to check equality between values. It has two parameters, second is optional. It may return true or false.
dec.equalTo(number[, base]);
The number
argument can be either a string/number variable or another BaseNumber instance. In case it´s a BaseNumber instance, base
parameter would be equal to the number instance's base
. E.g:
dec = Base(15);
hex = Base("f", 16);
dec.equalTo(hex); // returns true (base is hex.base())
hex.equalTo(dec); // returns true (base is dec.base())
In case number argument is a string/number variable, the base
parameter would be 10 by default, unless you specify it:
dec = Base(15);
hex = Base("f", 16);
dec.equalTo(15); // returns true (base is 10 by default)
hex.equalTo(15, 16); // returns false (base is 16)
Call the higherThan()
method to check if an element is higher than the argument number. It has two parameters, second is optional. It may return true or false.
dec.higherThan(number[, base]);
It works the same as the equalTo()
method.
Call the lowerThan()
method to check if an element is lower than the argument number. It has two parameters, second is optional. It may return true or false.
dec.lowerThan(number[, base]);
It works the same as the equalTo()
method.
Returns true if the number is negative, else returns false:
x = Base("-10")
y = Base(-0);
z = Base("100101", 2);
x.isNeg() // true
y.isNeg() // true
z.isNeg() // false
Returns true if the number is positive, else returns false:
const x = Base("-10")
const y = Base(-0);
const z = Base("100101", 2);
x.isPos() // false
y.isPos() // false
z.isPos() // true
Returns true if the number has not after-dot digits
x = Base("10.345")
y = Base(-0);
z = Base("100101", 2);
x.isInt() // false
y.isInt() // true
z.isInt() // true
Returns true if the number has after-dot digits
x = Base("10.345")
y = Base(-0);
z = Base("100101", 2);
x.isFloat() // true
y.isFloat() // false
z.isFloat() // false
Returns true whether the instance is 0
or '-0`, else returns false:
x = Base("0")
y = Base(-0);
z = Base("100101", 2);
x.isZero() // true
y.isZero() // true
z.isZero() // false
Returns true whether the instance is NaN
, else returns false:
x = Base("NaN", 8)
y = Base(NaN);
z = Base("100101", 2);
x.isNaN() // true
y.isNaN() // true
z.isNaN() // false
Returns true whether the instance is a finite value, else returns false. Non finite values are NaN
, Infinity
and -Infinity
, see Special values for more information:
x = Base("NaN", 8)
y = Base(Infinity);
z = Base("100101", 2);
x.isFinite() // false
y.isFinite() // false
z.isFinite() // true
Returns true whether the instance value is in x base. If x is omitted, it would be 10 by default:
x = Base("NaN", 8)
y = Base(Infinity);
z = Base("100101", 2);
x.isBase(8) // true
y.isBase() // true (base 10)
z.isBase(16) // false
Set the number of decimals you want to reach in the operations. Take into account that more decimals may affect time execution of the library. The maximum recommend number of decimals is 1000 without losing precision. By default, the value is set as 20:
Base.setDecimals(50);
Set the angle unit for trigonometric operations. there are two possible units: "degrees"
and "radians"
. By default, the value is set as "degrees"
:
Base.setAngle("degrees");
Base(90).cos().valueOf(); // 0
// or
Base.setAngle("radians");
Base.Pi.divide(2).cos().valueOf(); // 0
In case the Base
variable was used by another variable before loading the library, this function allows user to revert the Base
variable to the value it had before, and returns the BaseNumber constructor:
<script> let Base = 1 </script>
<script src='BaseNumber.js'></script>
<script>
const x = Base("ff", 16)
const B = Base.noConflict()
console.log( Base ) // 1
const y = B(10)
</script>
BaseNumber has 4 precision constants values that you can implement in your projects.You can reach up to 1025 digits of precision. Each constant is also a BaseNumber instance:
Base.Ln10.valueOf() // '2.302585092.....'
Base.Ln2.valueOf() // '0.693147180.....'
Base.Pi.valueOf() // '3.141592653.....'
Base.e.valueOf() // '2.718281828.....'
±0
, NaN
and ±Infinity
are valid BaseNumber values.
All values that are not NaN
in range (-Infinity, +Infinity) are considered finite values.
x = Base("NaN", 8)
y = Base(Infinity);
z = Base("-0", 2);
x.isNaN() // True
x.isFinite() // False
y.equalTo(1 / 0) // True
y.isFinite() // False
z.isNeg() // True
z.isFinite() // True