+1  A: 

You should always compare floating point numbers by using a constant (normally called epsilon) to determine much can two numbers differ to be considered "equal".

Lukáš Lalinský
+1  A: 

How about

function isEqual(a, b)
{
  tol = 0.01; // tunable 
  return Math.abs(a - b) < tol;
}
Ewan Todd
A: 

Use fixed-point math (read: integers) to do math where you care about that kind of precision. Otherwise write a function that compares your numbers within a range that you can accept as being "close enough" to equal.

Carl Norum
+1  A: 

This is a problem inherent in binary numbers that hits all major programming languages.

Convert decimal .1 (1/10) to binary by hand - you'll find it has a repeating mantissa and can't be represented exactly. Like trying to represent 1/3 as a decimal.

Joel Coehoorn
The problem is with binary floating point, not binary numbers in general. There are decimal floating point libraries out there (though not sure about in JS) that avoid this problem.
ScottJ
No, it's a problem with binary numbers. You can't represent .1 as a natural binary number. "decimal" libraries get around it by representing decimal digits or by using fixed-point decimals.
Joel Coehoorn
OK, true. What I should have said is that this problem does not affect binary integers.
ScottJ
+1  A: 

Just an idea. Multiply 10000 (or some similarly big number as long as its more than your max number of decimals) to all your values before you compare them, that why they will be integers.

function padFloat( val ) {
  return val * 10000;
}

padFloat( 0.1 ) + padFloat( 0.2 ) === padFloat( 0.3 );
Pete Duncanson
+2  A: 

You can of course multiply each number like

10 * 0.1 + 10 * 0.2 === 10 * 0.3

which evaluates to true.

Kimmo Puputti
+3  A: 

The best and only answer I have found that provides accurate results is to use a Decimal library. The BigDecimal java class has been ported to javascript, see my answer in this SO post.

Note: Scaling values will "treat" the issue but will not "cure" it.

Matt Baker