Ordinarily, JavaScript BigInt
division rounds towards 0. big-round
provides alternative methods for dividing BigInt
s with different rounding behaviour.
import { Divide, ROUNDING_MODE } from 'big-round'
const divide = Divide(ROUNDING_MODE.DIRECTED_AWAY_FROM_ZERO)
divide(25n, 10n) // 3n
roundingMode
must be one of the enumerated values provided by ROUNDING_MODE
(q.v.). Returns a new function divide(a, b)
, where a
must be a BigInt
, the dividend. b
must be a BigInt
, the divisor. The return value from divide
is the quotient, a BigInt
, rounded to the nearest integer using the provided rounding mode.
This object contains the following enumerated values. More detailed information about these modes of integer rounding can be found on Wikipedia.
If b
does not divide a
, instead of attempting rounding, throw an exception.
Always round fractions "down", towards zero. Note that this is the default rounding mode in JavaScript BigInt
division. big-round
provides it for completeness, but you can do a / b
to get an identical result.
Always round fractions "up", away from zero.
Always round fractions towards negative infinity. Equivalent to applying the floor function.
Always round fractions towards positive infinity. Equivalent to applying the ceiling function.
Round all fractions up or down randomly with probability proportional to proximity. E.g. a quotient of 1.7 is rounded down to 1n
with probability .3 and up to 2n
with probability .7.
Round fractions to the nearest integer. If the fraction is exactly .5, instead of attempting rounding, throw an exception.
Round fractions to the nearest integer. If the fraction is exactly .5, round "down", towards zero.
Round fractions to the nearest integer. If the fraction is exactly .5, round "up", away from zero.
Round fractions to the nearest integer. If the fraction is exactly .5, round towards negative infinity.
Round fractions to the nearest integer. If the fraction is exactly .5, round towards positive infinity.
Round fractions to the nearest integer. If the fraction is exactly .5, round to even.
Round fractions to the nearest integer. If the fraction is exactly .5, round to odd.
Round fractions to the nearest integer. If the fraction is exactly .5, round "up", away from zero, the first time this happens, then "down", towards zero, the second time, and continue to alternate in that fashion.
An internal counter on the divide
function tracks the number of times that this function has been called and the fraction turned out to be exactly .5. If the fraction is not exactly .5, the counter does not advance.
Round fractions to the nearest integer. If the fraction is exactly .5, round up or down with probability .5.