I was bored, so I started fidlling around in the console, and stumbled onto this (ignore the syntax error):
That raises a few questions for me:
- How in accurate is Javascript? Has this been determined? I.e. a number that can be taken into account?
- Is there a way to fix this? I.e. to do math in Javascript with complete accuracy (within the limitations of its datatype).
- Should the changed number after the second operation be interpreted as 'changing back to the original number' or 'changing again, because of the inaccuracy'?
I'm not sure whether this should be a separate question, but I was actually trying to round numbers to a certain amount after the decimal point. I've researched it a bit, and have found two methods:
> Method A
function roundNumber(number, digits) {
var multiple = Math.pow(10, digits);
return Math.floor(number * multiple) / multiple;
}
> Method B
function roundNumber(number, digits) {
return Number(number.toFixed(digits));
}
Intuitively I like method B more (looks more efficient), but I don't know what going on behind the scenes so I can't really judge. Anyone have an idea on that? Or a way to benchmark this? And why is there no native round_to_this_many_decimals function? (one that returns an integer, not a string)
0.025480999999999997
to 6 decimals you get0.025480
instead of the0.025481
you'd expect. Suddenly the compounded inaccuracy becomes 1 * 10^-6. That's a problem for me. I'd appreciate someone answering the actual question instead of telling me I'm wrong in what I need.. I think it's a valid problem. – Sudatoriumvar n = 0.025480999999999997; n.toFixed(6);
, I get0.025481
. What is your "actual question"? I count six questions in your post and we've answered most of them. – Hadfieldfloor()
would not get you a correctly rounded number because that's not whatfloor()
does. :) – Hadfield+numb.toFixed(digits);
instead in the future :) I think it's unfortunate I can't mark two answers as correct, that would be called for in this situation.. – Sudatorium