ultra-calc
v2.1.4
Published
Calculate beyond limitation.
Downloads
3
Maintainers
Readme
How to use
- Install the package ⇒
npm install ultra-calc
- Import the calculator function
import { calculate } from "ultra-calc";
// or
const calculate = require("ultra-calc");
- And calculate just like normal JavaScript syntax as
string
const result = calculate("23 - (4 + (-3) / 12) * 21 / 25 - 10 + (3 * 4 - 2)");
console.log(result); // ans: 19.85
That’s it.
How it differs from normal JavaScript Operations
- It can calculate large numbers (even thousands of digits long numbers)
const result = calculate(
"987234957093847598370 + (239845790238750923874509823745098327405987324905703294857023984570928374509823745982374095734905 / 9287409382740) * 2983740987304957023984750982374509823470582374590000000000000000000345 - 29834509872304985723045872930475982374509832745982374095"
);
console.log(result);
// result: 77054610761303457495377537739628744474993158843514754096923423496902414787516375035676103003727107112046862924698886045227428135917399697801885084675181.457375636706539876400288574192603245267117438939479398431107647404982275704352397629539447575752487572851901576280660267501957673695722883497326710844022772288667822665640928805072642880968918644366375816464346515420610278702663135578793987491278700829046082140982756478180274556798534029343284613518285565114165081801012165123836359581329058927211667559704365146269673696586969451684889947468149782790911020996998821049947432200209961647176223116670361381262081268925597562400534849581760819522093184020866826027951499289182070269378082207667694599997326358408820972846986802513410490053498132463437841592181469450571669933799518201209013587025416852201938558776730519114013511658899543206591723602253729589534340191287649244968658964055041411180820214190294755061027833267836820265817453658068443729664952508071527490681584951898874649563157671444967356466501473770695995945027780299119740318845717935431713774300870137633107739710498203843672447464001332270191020005003387912689408440409571013546 (truncated by 1000 fraction limit)
// this is just an example, you can go thousands of digits. Even millions of digits but it would take longer to respond.
- It provides accurate result
console.log(0.1 - 0.3);
// javascript result: -0.19999999999999998
console.log(calculate("0.1 - 0.3"));
// ultra-calc result: -0.2
Complexity
- Addition and Subtraction have O(N) complexity so it can handle million digits in a second normally.
- Multiplication and Division have O(N*N) complexity normally, but it depends on the multiplier, divisor, remainder, fraction quotient etc. Normally it can handle thousands of digits in a second.
Good to know
- By default, the fraction limit of division is 1000 (to avoid infinite fractions), so if any division requires more than 1000 decimal values to get the correct result, it might not work properly.
- It follows Operators priority as this: Division => Multiplication => Subtraction => Addition.`