This kind of blows my mind..
Turns out the output is "surprise!!". Doesn't this sort of undermine using JavaScript in some way if a program was going to use lots of floating point values?
This kind of blows my mind..
Turns out the output is "surprise!!". Doesn't this sort of undermine using JavaScript in some way if a program was going to use lots of floating point values?
One way of comparing floats could be by converting them to String values using toFixed() method
var fl1 = 0.11;
var fl2 = 0.11;
if ( fl1.toFixed(10) == fl2.toFixed(10) )
{
//same value
}
Javascript numbers are 64 bit floating point. see this ECMAScript specification.
http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.19
Floating point arithmetic in any programming language is prone to errors and is not always guaranteed to be accurate. see following for more on Floating point airthmatic
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
for more see the following QA
Is floating point math broken?
And yes its a WAT, but following rules can help you avoid errors
for currency store in cents instead of dollors.
54.67 as 5467 --- and divide by 100 before display.
For more read following post (its for c++ but will give insights on floating point erros)
http://www.codeproject.com/Articles/29637/Five-Tips-for-Floating-Point-Programming