Browse DevX
Sign up for e-mail newsletters from DevX


JavaScript: Playing the Numbers Game : Page 5

Built-in type-conversion in JavaScript makes using numbers so easy that developers rarely even think about using them. Surely that simplicity isn't as perfect as it seems! Just how good is the number support in JavaScript anyway? In this article you'll explore the edges of the world of numbers in JavaScript.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Five Drops of Wisdom: How to Stay Out of Trouble
  1. Most Web pages don't need fractional numbers. Avoid them and stick with integers. Make sure your array indexes are whole numbers. Count money in cents, not in dollars. Count percentages in whole percents, not in fractions. Minimize use of division (/) and modulo (%) operations, as they lead straight to floating point in most cases. If you must divide, use the Math.round method afterwards to get your integers back.

  2. If you can perform all the mathematics in your script as integer operations, your numbers will never contain any error. If both operands of the +, - and * operators are integers, and no extreme values occur, then your numbers will remain integers. This is the normal, everyday case. If you must rely on floating point, use guard digits—extra decimal places specified beyond those you actually need. If you require five-digit decimal accuracy, use six, or better yet eight decimal digits. Rounding error creeps into your five important digits far less rapidly when you use three guard digits. Do not rely on always using 17 decimal places. That won't always save you, for example, using 22 digits, all browsers display:

    1000200000000000000001 - 1000155000000000000001 ------------------------ = 44999999999967230

    In contrast, using (21 digits, all browsers, or merely 7 digits, if you remove the trailing zeros) results in:

    100020000000000000000 - 100015500000000000000 ----------------------- = 4500000000000000

    Despite their differences, both examples are correct to 10 significant figures or more.
  3. Do not use numbers too close together or too far apart. Subtracting or comparing two numbers that are very close together is highly error prone. Adding tiny numbers to large numbers is a waste of time, as the tiny number will just disappear to zero. Multiplying small and large numbers together is not a problem. For example, this line of code will not overflow the integer limits, or cause any accuracy problems, even though 12,345,678 is greater than 46,000:

    var total = 2 * 12345678; // = 24691356

    But the following line of code is likely to be inaccurate because the many significant digits are required to get the difference exactly right:
  4. var total = 0.1 - 0.09; // = 0.010000000000000009

  5. Check your results with isFinite() and isNaN(). If you submit any numerical data via a form, you should always do these checks beforehand, anyway:

    var data = document.forms[0].elements[0].value; if (data.isFinite() && ! data.isNaN()) data = parseFloat(data);

  6. Use calculations parsimoniously. The less mathematics you do, the less error will creep in. Treat your floating point numbers gently, keep them safe, and don't work them over and over. You browser (and your users) will love you.

Nigel McFarlane is a programmer, analyst and writer. His most recent book is "Rapid Application Development with Mozilla". Reach him by e-mail at nrm@kingtide.com.au.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date