Some math tasks require extreme precision. Imagine being just a little off when you calculate the trajectory to Mars, causing the spaceship to miss the planet entirely. Or think about how you’d feel if the interest calculation for your mortgage was imprecise (in the bank’s favor) or the ingredients in your prescription were proportioned slightly different from the doctor’s intent (enough to kill you).
Of course, the basic question that most people rightfully ask about now is why such libraries are necessary at all. The main culprit is rounding errors. Some numbers can’t be represented in some bases. For example, 1/3 presents a problem when working in base 10 because it’s a repeating decimal and there isn’t any way to represent it precisely. There are also irrational numbers such as π (pi—a value of approximately 3.1415926535897932384626433832795 – and that “approximately” is the first intimation of problems for some scientific calculations).
The approximations you get are quite usable for most purposes. Obtaining the precision output is time consuming, and making an application run a lot slower in order to obtain precision you don’t need doesn’t make sense.
The numbers.js library provides substantial math functionality for server-side use. You can use it to perform tasks in these categories:
- Basic calculations (such as adding two numbers)
- Complex numbers
- Digital Signal Processing (DSP)
- Matrix operations
- Computing prime numbers
- Generation calculations (such as Fibonacci)
The documentation with number.js is terrible, which is unfortunate because the library does provide a wealth of functionality. The download page isn’t much help either because it doesn’t provide you with a full list of functions. Follow these steps to see a full list of functions in the current version of numbers.js:
- Type node and press Enter at the node.js command prompt. You see a greater than sign (>) appear on screen.
- Type var Test = require('numbers'); and press Enter. This action loads the library so you can work with it.
- Type console.log(Test); and press Enter. You now see a full hierarchical list of functions as shown in Figure 1.
At this point, you can try out some of the functions. For example, let’s say you want to determine whether 5 is a prime number. In this case, you’d type console.log(Test.prime.simple(5)); and press Enter. The result is true, much as you’d expect. To test a specific function, you start with the name of the variable containing the library reference (Test), the name of the calculation module (prime), followed by the name of the function (simple), and end with any required input variables.
Download: Performed through node.js
Documentation: Pretty much non-existent
- Linear algebra
- Complex numbers
- ODE solver
- Unconstrained optimization
- PDE and sparse linear algebra
When looking at this page, you can change the values in the z = numeric.t(3,4); entry and see the new output immediately on screen. In fact, you can modify the equation. For example, changing it to z = numeric.t(3,4).x; displays just the x output. More important, the page shows precisely how you should use the function in your own code.
To access the hosted site in your own application, you add the requisite script entry to your page. The full version (the one with whitespace to make it human readable) is found at: http://www.numericjs.com/lib/numeric-1.2.6.js
Or you can use the minimum version (the one lacking whitespace and comments) to make things faster. This version is found at: http://www.numericjs.com/lib/numeric-1.2.6.min.js
Download: Hosted, none required
Precision already costs you a premium in the form of calculation time. However, adding to that cost by transferring whole page elements back and forth between the client and server makes things worse. The Tangle library helps you create reactive documents—ones that interact with the user in a contextual manner with extremely fast response times. The reactive nature of the page makes it possible for the user to interact with the math functionality in ways that would normally be possible only with a desktop application where everything is localized.
The unique aspect of Tangle is that it provides a simpler way to obtain input from the user. For example, a user might see a numeric value underlined on a page. When the mouse hovers over the value, a small balloon tip saying “drag” appears on screen. The user drags and finds that the value increases or decreases. A more interesting input method displays a graph containing the results of an equation. Moving the line on the graph automatically changes the equation content to match. Figure 3 shows a typical graph application. Notice the circle and associated balloon help where the mouse is hovered, which shows that you can drag the mouse to change the appearance of the graph (and the resulting equation).
To use Tangle, you either download the kit or create a series of links to the hosted site. Using the hosted site has the advantage of letting you get updates and fixes immediately and automatically; the downloaded version gives you speed of access and stability. Here are the links for the hosted version:
You also need to add styles to define which entries on a page can change. The Tangle CSS file helps you accomplish this task:
<link rel="stylesheet" href="http://worrydream.com/Tangle/TangleKit/TangleKit.css" />
The HTML for this example isn’t attached to a particular element. For example, you could easily put variables in a list, should you want to do so. However, you do need to identify how particular elements interact with Tangle, as shown here.
When adding numbers,
The container element, <p>, must have an id attribute so that Tangle can locate it on the page. Each variable element requires a data-var attribute with a unique name to identify it as well. When you want to make a variable user changeable, you assign it the TKAdjustableNumber class. The data-min and data-max attributes control the range of numbers that the user can select.
To make everything work, you need to create a script that defines the initial variable values, the actions that take place when a user changes a value, and the container element used to hold the variables. Here’s a simple script that completes the addition of two numbers scenario.
window.addEvent('domready', function ()
var model =
initialize: function ()
this.Value1 = 2;
this.Value2 = 3;
this.Result = 5;
update: function ()
this.Result = this.Value1 + this.Value2;
new Tangle(document.getElementById("DoAddition"), model);
The example begins by ensuring the page is actually loaded. When the page has completed loading, the code creates a model that defines the initial variable values and what happens during an update. This is a simple example; you could combine Tangle with any of the other math libraries to create incredibly complex update functions, and the updates can occur in multiple stages. The final step is to create the actual Tangle object and to assign the model you’ve defined to the container holding the variables.
Documentation: http://worrydream.com/Tangle/guide.html and http://worrydream.com/Tangle/reference.html
To use this library, you can either download the code from the site or you can use the hosted version. Here’s the link for the hosted version: http://josscrowcroft.github.io/accounting.js/accounting.js
This library is solely used to format output and unformat input. As a consequence, it’s really lightweight. The documentation for this library is quite good and using the library is relatively simple. Here’s a script that takes a numeric value and outputs it as a monetary value using the local symbols and formatting methodology.
should appear as a monetary amount.
As you can see, there are no objects to create, no weird scripts to provide. All you do is call the requisite function to format the output as needed. Simple doesn’t mean lack of flexibly, however. This library does give you full control over the appearance of the output through the use of optional parameters.
A single article can’t provide you with a complete list of all the math libraries available. You can probably find a library for most needs. A good place to start such a search is the JSter site or the JS Wiki of math libraries which provide individualized summaries. The two things you need to consider as highest priority are getting the right result and getting that result easily. It doesn’t matter how nice the library is if you don’t get the right result and making things hard only increases the chances of human error.