A BigNumber.js Library of Mathematical Constants and Functions
Functions and BigNumber properties for computing various mathematical functions.
This is a supplementary library of maths Javscript functions and relies entirely on
Michael Mclaughlin's arbitrary-precision BigNumber.js code.
To use this LIBrary, download the file bignumberLIB.js and include it in your JavaScript
code file
or else use a direct link in your HTML file:
<script type='text/javascript' src='http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/BigNumberLIB/bignumberLIB.js'></script>
Please send comments, corrections and suggestions to the author: Dr Ron Knott. Details at the foot of this page.
Contents of this page
A JavaScript (JS) library add-on to BigNumber.js
All arguments can be either a JS Number or else a BigNumber unless otherwise indicated.
All results are BigNumbers unless otherwise indicated.
THIS refers to the BigNumber on which a property is being applied.
Precision
BNsetdps(n)
Sets the BigNumber Working precision (DECIMAL_PLACES) to n (a JS number) decimal places.
BNdps()
Return the current number of DECIMAL_PLACES from BigNumber (a JS number in the range 0 to 1e9).
Shorthand for BigNumber.config().DECIMAL_PLACES
Constants
All the following are BigNumbers.
BN0, BN1, BN2, BN3, BN5, BN10, BNinf
0, 1, 2, 3, 5, 10, ∞ as BigNumber (constants)
BNm1, BNminf
-1 and -∞ as BigNumber (constants)
BNpi, BNe, BNphi, BNEulerGamma
These are stored with 200 dps accuracy. They are probably
best used by rounding them to the precision you are using, for example:
BNpi.toPrecision(BNdps()) or else by recomputing them as indicated.
π = Pi = 3.14159... to 200 dps accuracy.
If you want more or less accuracy, set the Working Precision to the number of dps required then use
BN1.arctan().mul(4) for Pi.
e = E = 2.71828... to 200 dps accuracy.
BN1.exp() will recompute e to your current BigNumber precision.
φ = golden section number = (1+√5)/2 = 1.61803398874989484821...
BN1.add(BN5.sqrt()).div(2)
γ = 0.5772156649... to 200 dps accuracy
Integer Functions
fw(N,fieldwidth)
N.toString() is returned, padded with spaces on the left to make it fieldwidth (a JS positive integer)
characters long if necessary.
N can be any JS object but this function is useful for formatting the output of integers.
See the Pascal's Triangle example in the interactive Calculator below.
.inc()
Returns THIS.add(1)
.toBase(b)
Returns THIS as a string of digits in base b (2 to 36, a JS Number).
.toBaseDigits(b)
Same as .tobase(b) for base 2 to 64 but returns an array of the digits (JS Numbers).
BNgcd(x,y) Alias: BNGCD
Returns the GCD of x and y
BNegcd(x,y)
The Extended GCD of x and y.
Return an array of 3 BigNumbers: [a,b,g] where
a x + b y = g , the GCD of x and y.
BNgcd3(x,y,z)
Returns the gcd of x, y and z.
BNlcm(x,y)
The LCM of x and y.
.sqr()
Returns the square of the BigNumber.
.factorial()
The factorial of the BigNumber. 0!=1. THIS must be a non-negative integer.
.factorialNbDigits(B)
The number of 'digits' in the factorial of THIS when expressed in base B;
OR... the base B log of the factorial of THIS, plus 1. It returns a BigNumber so that the fractional part can be used
to estimate the first few digits.
The base B is any JS number or 10 if omitted.
This is considerably quicker than .factorial().log(B).inc().floor() as it uses a good approximation formula.
BNbincoeff(r,c), BNbinomial(r,c)
r and c are JS Number.
Return the Binomial coefficient of row r and column c in Pascal's triangle if r≥0 and c≥0 but 0 otherwise.
This is the number of ways to choosing a sequence (a list where the order matters) of c things from r available.
"from r choose c": _{r}C_{c} =
(
r
)
=
r!
c
(r-c)! c!
BNrandomInt(lo,hi)
Return a random integer in the range lo..hi inclusive, where each is equally likely.
Comparison functions
These are two letter aliases for the BigNumber built-in properties.
.ne(N)
Not equal to: THIS != N
.le(N)
Same as lte: THIS ≤ N
.ge(N)
Same as gte: THIS ≥ N
BNeq(x,y,error)
Are two numbers/BigNumbers equal to within a given error (+ or -)?
If error is omitted, it is assumed to be 0.
BNmax(Array)
The maximum element of an array of Numbers and BigNumbers
BNmaxabs(Array)
The maximum element of the array of Numbers and BigNumbers, if comparisons are performed on the size of the elements, that is,
ignoring their signs. The computed element within the Array, positive or negative, is returned.
Powers and Logs
.log(b)
for b>0, the log to base b (JS Number or BigNumber).
BNlog(x)
The natural log (base E) of x.
.powmod(p,m)
this to power p reduced mod m.
p can be any power whereas the BigNumber built-in .toPower is limited to a JS integer p in the range ±9007199254740991.
.topow(p)
THIS to the power p.
p can be any power whereas the BigNumber built-in .toPower is limited to a JS integer p in the range ±9007199254740991.
.exp()
E to the power THIS.
Trig and inverse Trig
.degtorad()
convert THIS as radians to degrees
.radtodeg()
convert THIS as degrees to radians
.sin()
the sine of THIS in radians. The result is in the range -1 to 1.
.cos()
the cosine of THIS in radians. The result is in the range -1 to 1.
.sec()
the secant of THIS in radians where secant(A) = 1/sin(A).
.cosec()
the cosecant of THIS in radians where cosecant(A) = 1/cos(A).
.tan()
the tangent of THIS in radians.
.cot()
the cotangent of THIS in radians where cotangent(A) = 1/tangent(A) .
.arcsin()
the angle in radians (-Pi/2 to Pi/2) which has THIS as its sine. THIS must be in range -1 to 1.
.arccos()
the angle in radians (0 to Pi/2) which has THIS as its cosine. THIS must be in range -1 to 1.
.arctan()
the angle in radians (-Pi/2 to Pi/2) which has THIS as its tangent.
.arccot()
the angle in radians (-Pi/2 to Pi/2) which has THIS as its cotangent
Hyperbolic trig and inverses
.sinh()
sinh(x) =
e^{x} - e^{-x}
2
.cosh()
cosh(x) =
e^{x} + e^{-x}
2
The result is always at least 1.
.tanh()
tanh(x) =
sinh(x)
=
e^{x} - e^{-x}
=
e^{2x} - 1
cosh(x)
e^{x} + e^{-x}
e^{2x} + 1
. The result is in the range -1 to 1.
.arctanh()
the inverse of tanh, the number whose tanh is THIS. THIS must be in the range -1 to 1.
.arcsinh()
the inverse of sinh, the number whose sinh is THIS.
.arccosh()
the inverse of cosh, the number whose cosh is THIS, which must neither be negative nor less than 1.
An Interactive Calculator
Use write(expressions) to output each argument's value in the RESULTS area.
The value of each argument is converted to a string, the values are joined and output on a single line.
Use "<br>" or "\r" to start a newline.
For example:
write("cos(Pi/3) = ",BNpi.div(3).cos())
write("Root 2 = ",BN2.sqrt())
Pascal's Triangle
var n=10000;write("Factorial ",n," has ",(new BigNumber(n)).factorialNbDigits()," digits")
BigNumber C A L C U L A T O R
Javascript code:
R E S U L T S
Working precision is
decimal places
For an example of the use of these functions with an expression parser, see the red EXPRESSION box on this page (opens in a new page)
where you can type in a maths expression in the usual form (not the raw BigNumber functions as on this page)
and see the results as well as dynamically set the number of decimal places
you want for the results.
About this LIBrary
Speed
The LIBrary is a first attempt and is reasonably efficient/fast for up to several hundred dps. The slowest function is log
and it is also used in arcsinh, arccosh, arctanh.
Any offers of help to improve the efficiency of this code are very welcome! (See my email at the foot of this page.)
Errors
A Javascript alert is generated when an error is found and then the script evaluation STOPs.
This should probably be updated to throw specific JavaScript Errors.
Dr Ron Knott
This Help page was created on 12 August 2019 and updated on
Hosted by the Mathematics Department of the University of Surrey
where I was a lecturer for many years and am now a Visiting Fellow.
With thanks to Michael Mclaughlin for creating his BigNumber.js library
(see MIT's GitHub licence).