Going Beyond the Math Object’s Capability
Problem
The Math object provides good, basic mathematical functionality, but lacks advanced
or business-specific math functionality that you need. In addition, Math does everything
in floating point, and you need functions that work to a higher degree of precision.
Solution
Use a library that expands on the Math object’s capability. Examples of these libraries
and their usage are covered in the discussion.
EXPLAIN
Most of our math functionality is satisfied by the built-in Math object. However, there
are instances where what it provides, or doesn’t provide, leaves gaps. That’s where a small
but powerful set of Math libraries and/or modules comes in. I’ll cover some options in
this section.
Math.js
The Math.js library can be installed using npm or Bower, downloaded directly from its
website, or accessed via content delivery network (CDN). It can be used in the browser,
or in Node applications. It provides a set of functions to perform operations, such as
add() and multiply(), that have the added advantage of being chainable:
var result = math.select(9) .add(3) .subtract(6) .multiply(23) .done(); // get value console.log(result); //{ value: 138 }
It also provides functionality to parse a mathematical expression, with its own version of eval():
var exp = "4 + 3 * 10 / 8"; console.log(math.eval(exp)); // 7.75
In addition, it supports matrices. For example, to create a [3,3] matrix:
var m = math.matrix([[4,3,2],[6,6,8],[7,4,5]]); console.log(m.valueOf()); //[ [ 4, 3, 2 ], [ 6, 6, 8 ], [ 7, 4, 5 ] ]
Note that the matrix arrays are contained within an outer array. Use the following to create a zero-filled matrix:
var z = math.zeros(2,2); console.log(z.valueOf()); // [ [ 0, 0 ], [ 0, 0 ] ]
Most of the Math.js functions require the valueOf() or done() function to actually get the value of the operation, as noted in the code snippets. Math.js also provides support for BigNumbers, numbers that have arbitrary precision, as well as complex numbers, with both real and imaginary parts:
var b = math.complex('4 - 2i'); b.re = 5; console.log(b.valueOf()); // 5 - 2i
Accounting
There are several libraries and modules for providing accounting capability, but argu‐
ably the most popular is Accounting.js, maintained by Open Exchange Rates (whose
currency conversion API is introduced ). Like many other libraries, it can
be downloaded from its main site, accessed via CDN, or installed using npm:
npm install accounting
You can use Accounting.js to format a number into a currency format:
var options = { symbol : "$", decimal : ".", thousand: ",", precision : 2, format: "%s%v" }; // Example usage: var m = accounting.formatMoney(45998307); console.log(m);// $45,998,307.00
You can also format entire columns of numbers:
var list = [[456, 12, 3], [99, 23,3],[667,902,12]]; var c = accounting.formatColumn(list); console.log(c);[ [ '$456.00', '$ 12.00', '$ 3.00' ], [ '$99.00', '$23.00', '$ 3.00' ], [ '$667.00', '$902.00', '$ 12.00' ] ]
The formatting isn’t all U.S. dollar–based either:
var p = accounting.formatMoney(4899.49, "€", 2, ".", ","); console.log(p); // €4.899,49
The Accounting.js functionality isn’t extensive, but what it does, it does well.
Advanced Mathematics and Statistics-A popular advanced math module in Node is Numbers, installed as:
npm install numbers
You can also download or install it on the client using Bower. The library provides advanced calculus functions, matrix math, and even some statistics. From the documentation:
numbers.statistic.mean(array); numbers.statistic.median(array); numbers.statistic.mode(array); numbers.statistic.standardDev(array); numbers.statistic.randomSample(lower, upper, n); numbers.statistic.correlation(array1, array2);
I’ll leave it for the more mathematically inclined to explore all the functionality.
No comments:
Post a Comment