I've ran into several oddities using javascripts floating point arithmetic, but I can never recall them off the top of my head!
What are some common mistakes when using JavaScript to do math?
I've ran into several oddities using javascripts floating point arithmetic, but I can never recall them off the top of my head! What are some common mistakes when using JavaScript to do math? |
|||
|
The common problems with javascript arithmetic relate to the use of Not using radix when converting strings to integers:
Not using
Misunderstanding floating point arithmetic (applies to all languages, not just JS):
Calculating currency with floating points may lead to rounding errors, currency values should be treated as integers (multiplied with 100) before processing. |
||||
|
A common mistake (although with strings & integers, not floats) is forgetting that JavaScript doesn't have strong typing. So you can run into situations where:
All of these are perfectly valid statements in JavaScript. But it gets weirder, because while "7" and "cat" are both strings, not all strings are treated the same:
Not to mention that (as @Tatu wrote):
|
|||
|