-1

I'm learning Javascript at the moment and the particular lesson that I'm on right now is showing how to turn 1.075 into 7.5% for display purposes.

the math looks as follows:

(1.075 - 1) * 100

and this is displaying in the results as 7.499999999999996. Why in the world is it calculating like this in Javascript? Every calculator that I used to do the exact same math came up as 7.5, as it should. I didn't even need a calculator to realize something was odd, but I wanted to run the same numbers on as many calculators as I could just to reassure myself. I'm actually shocked that the guy doing the tutorials didn't say a single thing about that, other than how to fix it to display only 2 decimal places, because this just seems so odd. So what be going on? I'm crazy curious about what's going on here.

Thanks!

I want to apologize to everyone that was obviously very upset by my asking a duplicate question, and decided to downvote me for it. I would have been just as well searching for an answer, only I don't even know how I could have began to query for such a question.

To the people that answered, and linked me to another article answering my question; thank you very much, and I apologize for the duplicate :)

Soundfx4
  • 595
  • 2
  • 8
  • 20
  • 12
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Habib Mar 07 '14 at 20:40
  • 4
    We shouldn't dump downvotes on duplicate questions. See [this meta post](http://meta.stackexchange.com/questions/62819/should-we-downvote-duplicates). – Robbie Wxyz Mar 07 '14 at 20:41
  • 1
    Try representing `.075` in binary and see how it goes. Also note that `7.499999999999996` is `7.5`, but it is not `7.500000000000000`. – David Mar 07 '14 at 20:43
  • 1
    1075/1000 is periodic number in binary (1.000 1001 1001 1001 ...) just like 1/3 is a periodic number in decimal (0.3333...). As such, it cannot be represented exactly using floating point numbers. – ikegami Mar 07 '14 at 20:43
  • I appreciate your comment, SuperScript. It indeed is not right. I'm not even sure how I could begin to know how to search for an answer to this question. And I appreciate everyone else's comments explaining this as well :) – Soundfx4 Mar 07 '14 at 20:43
  • 1
    @SuperScript No they shouldn't. I didn't downvote, but it doesn't surprise me too much that others did. Not because it's a duplicate, but because the question doesn't show a great deal of research effort, given that there are dozens of other questions just like this on SO and a large number of off-site resources discussing this topic. That said, if the tutorial OP is reading didn't make *any* mention of this, then OP should probably look for a better tutorial site. – p.s.w.g Mar 07 '14 at 20:49
  • @p.s.w.g I'm sorry, but how can one successfully find an answer if they are not sure what questions to ask? I could easily enough put the question into words that another Human Being could understand, but I didn't even know where to BEGIN to be able to put the question in a search string that would return relative results. That being said, this tutorial could be better, but it is only one of many tutorials that I will be using and it is teaching me. The lack of detail forces me to pause the video and examine the code to try to fill in the gaps. In a way, I think that's helpful. – Soundfx4 Mar 07 '14 at 20:59
  • @p.s.w.g also, the guy did explain how to display it with exactly 2 decimal places. I mentioned that in the OP though. – Soundfx4 Mar 07 '14 at 20:59

1 Answers1

1

Some decimal numbers cannot be accurately stored as floating point numbers since they are periodic in binary. 1075/1000 is such a number.

http://floating-point-gui.de/

ikegami
  • 367,544
  • 15
  • 269
  • 518
jgitter
  • 3,396
  • 1
  • 19
  • 26