1

Possible Duplicate:
Is JavaScript's Math broken?

If(0.3-0.2==0.2-0.1) most of programming language showing result false.So I want to know which Language Support this statement true and why most of language shows false.

Tested in Javascript and PHP. Why does this happen?

Community
  • 1
  • 1
John
  • 159
  • 11
  • At first glance this sounds like a contentious question, however rewording to expose the real meat makes it sound less so (I believe the real meat deals with using a floating point approximation rather than a rational representation). I have edited appropriately. – Kristopher Micinski Oct 05 '12 at 04:05
  • 1
    @prashanth possible, but the OP also asks which langues support this by default. Though I agree there is a definitely striking resemblance .. – Kristopher Micinski Oct 05 '12 at 04:12

1 Answers1

4

The reason that this happens is not to do necessarily with a language, per se, but an implementation. The answer to this question is that when you write If(0.3-0.2==0.2-0.1), you are thinking that the operations performed are in the rationals, this is false, they are indeed in floating point arithmetic. Indeed, within floating point arithmetic, this is false! So the answer is: any language which defaults to interpreting constants in an arbitrarily precise arithmetic. (For example, if these had been written in Haskell using a Rational number, then the test would have "worked.") For completeness: the Racket language does this.

Kristopher Micinski
  • 7,572
  • 3
  • 29
  • 34