1

I'm using Chrome, version 37.0.2062.124 m, and FireFox 22.0 and the computation gives me wrong result:

5*0.1 //the result is 0.5
6*0.1 //the result is 0.6000000000000001, it should be 0.6
7*0.1 //0.7000000000000001
8*0.1 //0.8

If possible to answer, how did they come up with this? If not, is there any library that does the job? I don't need new data type, float is enough, I just want this to get the correct result.

kazinix
  • 28,987
  • 33
  • 107
  • 157
  • If you are truly in need of calculations with high precision, see: http://blog.smartbear.com/testing/four-serious-math-libraries-for-javascript/ – JLRishe Oct 10 '14 at 03:54
  • A common work-around is to round to a certain number of decimal points of accuracy when displaying the result and the tiny discrepancy will go away. – jfriend00 Oct 10 '14 at 04:01
  • 1
    You need to read What Every Computer Scientist Should Know About Floating Point Numbers. You should also know that it's not a language or library flaw. You can no more represent 0.1 exactly in binary than you can 1/3 in decimal. – duffymo Oct 10 '14 at 08:59

0 Answers0