bigarith.js
Version:
Do very large math to precision!
59 lines (42 loc) • 6.59 kB
JavaScript
console.log("%cTesting for subtract", "color: blue; font-size: 20pt;");
var x = new BigArith("0.5");
var y = new BigArith("zero point five");
var z = new BigArith(0.5);
var a = new BigArith(".5");
var b = new BigArith("point five");
var c = new BigArith(5e-1);
//You should be able to subtract words, strings or numbers from a BigArith object using the subtract() function
var result = x.subtract("point five").subtract(.5).subtract(".5").subtract(5e-1);
assertDeepEqual(result, new BigArith(-1.5)); // => "-1.5" = 0.5 - 0.5 - 0.5 - 0.5 - 0.5
//You should be able to subtract BigArith objects each other using the subtract() function
result = x.subtract(y).subtract(z);
assertDeepEqual(result, new BigArith(-0.5));// => "-0.5" = 0.5 - 0.5 - 0.5
//You should be able to take a result and still substract from it
result = result.subtract(2)
assertDeepEqual(result, new BigArith(-2.5)); // => "-2.5" = -0.5 - 2
/* The subtract() function always result a BigArith object
* Get the value of your result as a string using the valueOf() function*/
assertEqual(result.toString(), "-2.5"); // => "-2.5"
//You should be able to substract BigArith constants from each other
var ba = new BigArith("PI");
assertDeepEqual(ba.subtract(new BigArith("SQRT2")), new BigArith("1.72737909121669818966095465906980480562749752399815774779826485431708392782410195977764729101447549524713424028237000962216900855369984501908123791102854528603706143677443292834975969412179617286981049")); //PI - SQRT2
var then = new Date();
ba = new BigArith("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999");
assertDeepEqual(ba.subtract("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"), new BigArith("0"));
var now = new Date();
console.log(`Subtracting two ${ba.toString().length} digits took ${now-then} milliseconds`);
then = new Date();
ba = new BigArith("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999");
assertDeepEqual(ba.subtract("2"), new BigArith("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997"));
now = new Date();
console.log(`Subtracting a single digit from a ${ba.toString().length} digits took ${now-then} milliseconds`);
ba = new BigArith("1001");
assertDeepEqual(ba.subtract("-1"), new BigArith("1002"));
assertDeepEqual(BigArith.subtract("1001","-1"), new BigArith("1002"));
assertDeepEqual(BigArith.subtract("1001","1"), new BigArith("1000"));
assertDeepEqual(BigArith.subtract("-1001","-1"), new BigArith("-1000"));
assertDeepEqual(BigArith.subtract("-1001","1"), new BigArith("-1002"));
assertDeepEqual(BigArith.subtract("0.9","0.1"), new BigArith("0.8"));
assertDeepEqual(BigArith.subtract("1","-0.1"), new BigArith("1.1"));
var ba = new BigArith("3");
console.log(ba.subtract(NaN));