View on GitHub

bigarith.js

Do very large math to precision!

divide()

divide() returns the quotient of the division of two numbers. There is a method function and a static method function.

Syntax

method function
ba.divide(n);
static method function
BigArith.divide(a, b);

Parameters

method function

n - Required - {string|number|BigArith}

The divisor with the value of the BigArith object as the dividend. This could be a string of digits, a number, or a BigArith object.

static method function

a - Required - {string|number|BigArith}

The dividend: this could be a string of digits, a number, or a BigArith object.

b - Required - {string|number|BigArith}

The divisor: this could be a string of digits, a number, or a BigArith object.

Return value

method function - {BigArith}

A BigArith object with its value equals to the quotient of the division of the value of the BigArith object it is called on (the dividend) and n (the divisor).

static method function - {BigArith}

A BigArith object with its value equals to the quotient of the division of a (the dividend) and b (the divisor).

Description

There are two functions which could be used, the method function, and the static method function. The method function takes one parameter (n) as the divisor and returns the quotient of the division of the value of the BigArith object it is called on and n.

The static method function takes two parameters (a, b) and is always used as BigArith.divide(). It returns the quotient of the division of a as the dividend and b as the divisor.

The quotient returned will be to maximum of 200 decimal places when necessary.

If n (in case of method function) or b (in case of static method function) is equivalent to zero, a RangeError("Division by zero") will be thrown.

Any number parameter (that is not strings of digits or a BigArith) should be between the Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER limits.

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");
ba = ba.divide("24011985"); //BigArith object with value "-0.70931187071789358522421199246959382991451977002317800881518125219551819643398911002151633861173909612220730605986968590893256013611536072507125087742641851558711201926871102076733764409731223803446487"

ba = new BigArith("4");
ba = ba.divide("0.5"); //BigArith object with value "8"

ba = new BigArith("8888888888888888888888888888888888888888888888888888888");
ba = ba.divide("99999999999999999999999999999999999999999999999999999999999999"); //BigArith object with value "0.00000008888888888888888888888888888888888888888888888888888888000000088888888888888888888888888888888888888888888888888888880000000888888888888888888888888888888888888888888888888888888800000008888889" 

Using the static method function

var ba = BigArith.divide("-17031986", "24011985"); //BigArith object with value "-0.70931187071789358522421199246959382991451977002317800881518125219551819643398911002151633861173909612220730605986968590893256013611536072507125087742641851558711201926871102076733764409731223803446487"
ba = BigArith.divide("4", "0.5"); //BigArith object with value "8"
ba = BigArith.divide("8888888888888888888888888888888888888888888888888888888", "99999999999999999999999999999999999999999999999999999999999999"); //BigArith object with value "0.00000008888888888888888888888888888888888888888888888888888888000000088888888888888888888888888888888888888888888888888888880000000888888888888888888888888888888888888888888888888888888800000008888889"

Method chaining

Since the method returns a BigArith object, method chaining is possible.

var ba = new BigArith("-17031986");
ba = ba.divide("+17031986").add("24011985").multiply("456785564").subtract("2"); //BigArith object with value "10968327654198974"

More examples here. Full documentation here

See also