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.



Sets the BigNumber Working precision (DECIMAL_PLACES) to n (a JS number) decimal places.


Return the current number of DECIMAL_PLACES from BigNumber (a JS number in the range 0 to 1e9).
Shorthand for BigNumber.config().DECIMAL_PLACES


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...
γ = 0.5772156649... to 200 dps accuracy

Integer Functions


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.


Returns THIS.add(1)


Returns THIS as a string of digits in base b (2 to 36, a JS Number).


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


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.


Returns the gcd of x, y and z.


The LCM of x and y.


Returns the square of the BigNumber.


The factorial of the BigNumber. 0!=1. THIS must be a non-negative integer.


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": rCc = (r) = r!
c (r-c)! c!


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.


Not equal to: THIS != N


Same as lte: THIS ≤ N


Same as gte: THIS ≥ N


Are two numbers/BigNumbers equal to within a given error (+ or -)?
If error is omitted, it is assumed to be 0.


The maximum element of an array of Numbers and BigNumbers


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


for b>0, the log to base b (JS Number or BigNumber).


The natural log (base E) of x.


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.


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.


E to the power THIS.

Trig and inverse Trig


convert THIS as radians to degrees


convert THIS as degrees to radians


the sine of THIS in radians. The result is in the range -1 to 1.


the cosine of THIS in radians. The result is in the range -1 to 1.


the secant of THIS in radians where secant(A) = 1/sin(A).


the cosecant of THIS in radians where cosecant(A) = 1/cos(A).


the tangent of THIS in radians.


the cotangent of THIS in radians where cotangent(A) = 1/tangent(A) .


the angle in radians (-Pi/2 to Pi/2) which has THIS as its sine. THIS must be in range -1 to 1.


the angle in radians (0 to Pi/2) which has THIS as its cosine. THIS must be in range -1 to 1.


the angle in radians (-Pi/2 to Pi/2) which has THIS as its tangent.


the angle in radians (-Pi/2 to Pi/2) which has THIS as its cotangent

Hyperbolic trig and inverses


sinh(x) = ex - e-x


cosh(x) = ex + e-x

The result is always at least 1.


tanh(x) = sinh(x) = ex - e-x = e2x - 1
cosh(x) ex + e-x e2x + 1
. The result is in the range -1 to 1.


the inverse of tanh, the number whose tanh is THIS. THIS must be in the range -1 to 1.


the inverse of sinh, the number whose sinh is THIS.


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:
 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


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.)


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 enquiry (at) ronknott (dot) com
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).