0

I just noticed the strangest problem with simple addition in javascript.

I have a simple equation that I am doing:

-1.000+(1.001) and the answer should be .001 however I am getting 0.0009999999999998899 instead. I can't understand why this is happening and there doesn't seem to be any way to get the right answer. I've check with multiple calculators and they all give me .001 but javascript gives me this crazy number.

What is going on? This isn't right. The number is very close but it is completely wrong. I've tried with other decimals values and I get strange results. How can javascript not do math properly?

Here is the simple alert box I used:

alert(1.000-(1.001));
RandyL
  • 17
  • 7
  • 1
    See, this is why SO has ten million questions. It's because eight million of them are from people who couldn't be bothered looking up floating point inaccuracy before posting :-) See http://powerfield-software.com/?p=30 for one explanation. – paxdiablo Sep 14 '15 at 06:29
  • http://stackoverflow.com/questions/588004/is-floating-point-math-broken check this out. – kdlcruz Sep 14 '15 at 06:30
  • @paxdiablo: That is not a very friendly way to put it. Most of us have asked questions like this at one time or another. – Michael Geary Sep 14 '15 at 06:33
  • @RandyL: The calculators you checked with probably do not use IEEE754 floating point arithmetic like JavaScript and most other programming languages. They may operate in base 10 instead of binary, for example. In binary floating point, a number like 0.001 has no exact representation, while in base 10 it has the exact value you expect. Wolfgang's answer has a solution that will handle most of the cases you might be interested in: simply use `.toFixed()` to round the number to the number of digits you want - but keep in mind that there will always be rounding errors like this. – Michael Geary Sep 14 '15 at 06:38
  • @MichaelGeary, the smiley *clearly* called it out as humour, and I even went so far as to suggest a site for further info. But, in any case, you don't jump into a forum without at least trying to understand the guidelines any more than you jump into a conversation without spending some time learning the context. *That's* what I'd be more likely to consider rude, rather than a gentle jab at someone for not bothering to research a little first. If Randy has an issue, I'll be happy to rephrase/adjust/explain my comment. – paxdiablo Sep 14 '15 at 06:40
  • @paxdiablo: FWIW, I did get a chuckle out of your comment. I just wonder if I would have felt the same way if I were the *target* of the humor. (BTW, the other two million questions are people wondering why you can't call a jQuery function when you haven't loaded jQuery.) Randy, don't be put off by any of our warped senses of humor. Hopefully you'll get used to it soon enough. Cheers everyone! :-) – Michael Geary Sep 14 '15 at 06:50
  • @Randy, FWIW, even my *wife* of over a decade isn't used to my sense of humour yet :-) So I'd hardly have a right to expect someone I've just encountered on the net to be any better. No offence was intended, hopefully you didn't take it that way, and sincere apologies if you did. – paxdiablo Sep 14 '15 at 06:53
  • @paxdiablo Sorry for asking a question twice..I looked and didn't find the answer. Perhaps I didn't know what the problem was so I wasn't able to ask the right question. I am, after all, new to javascript and this site. – RandyL Sep 15 '15 at 14:43
  • @paxdiablo Thanks for the followup and for the link. I finally got the issue resolved :) – RandyL Sep 15 '15 at 14:53

1 Answers1

0

try it with toFixed() method.

var erg = 1.000-(1.001)
alert(erg.toFixed(3));
Wolfgang Müller
  • 386
  • 1
  • 6
  • 21