Javascripts numeric type is extremely unpredictable with regards to significant figures and sometimes rounding. This JSFiddle includes a good example of the problem where the value 0 can never be reliably reached in a series of addition and subtraction operations (and other suspect results are also visible).
What rules can I follow to reliably produce correct floating point arithmetic results in javascript?
For reference, the JSFiddle content is contained below.
<input id="input" type="number" value="0" />
<button id="plus" type="button">+</button>
<button id="minus" type="button">-</button>
function modify(direction) {
var modifier = direction * 0.10;
var next = Number($('#input').val()) + modifier;
$('#input').val(next);
}
$('#plus').on('click', function () {
modify(1);
});
$('#minus').on('click', function () {
modify(-1);
});