whatgoodisaroad / Big-js

An arbitrary precision math library for JavaScript
http://big-js.thatscaptaintoyou.com/
44 stars 6 forks source link

Inverse of big numbers #5

Open alatcache opened 11 years ago

alatcache commented 11 years ago

If you try 1/1e{DP+1} using Big.js, where DP is the current value of Big.DP, the result is zero. This inverse function seems to get round the problem ...

function big_inv(number) { var nu = new Big(String(number));

// Create 1 for inverse var one = new Big('1');

// A Big for the result var res = new Big('1');

// Get the number's coefficient var coef_nu = '0.' + nu.c.join(''); var coef = new Big(coef_nu);

// Invert that var inv_coef = one.div(coef);

// Change sign of exponent and put into result res.e = (-1 * nu.e) - 1; res.c = inv_coef.c; return res; }