-1

I'm working on a project and I noticed some strange values. I did some tests with various input values and ended up with this - what seems to me - weird behavior:

1 + 0.72 = 1.72 // expecting 1.72
2 + 0.72 = 2.7199999999999998 // 2.72
1 + 1.72 = 2.7199999999999998 // 2.72
1 + 2.72 = 3.72 // 3.72
1 + 3.72 = 4.720000000000001 // 4.72

Does anyone have a clue on what's going on and how to prevent this, that is, how do I make sure I get what I'm expecting?

Alexei
  • 672
  • 1
  • 5
  • 13
  • Welcome to floating point maths! – Jakub Konecki Aug 21 '13 at 12:06
  • 1
    This is not just javascript. – Benjamin Toueg Aug 21 '13 at 12:07
  • 1
    As a self answered question this is somewhat lacking. I would expect more effort to make the answer canonical (and check for dupes first) – Martin Smith Aug 21 '13 at 12:08
  • Binary floating-point numbers are inept at handling decimal fractions, so 0.1 + 0.2 is not equal to 0.3. This is the most frequently reported bug in JavaScript, and it is an intentional consequence of having adopted the IEEE Standard for Binary Floating-Point Arithmetic (IEEE 754) – Carlos Morales Aug 21 '13 at 12:11
  • I did search the Internet first but, then again, what the heck am I supposed to search for to get an answer? I understand now that my query was not broad enough since I never thought about floating point math being broken. Also, the only reason I answered my own question was that while I was typing the question, I figured out I could use the toFixed method to get what I want. In the end, I asked for 2 things: why is this happening, and how do I fix it. I only answered the second. – Alexei Aug 21 '13 at 12:14
  • @Alexei: There's nothing wrong with including an attempted fix ("answer") in the question. You don't have to distinguish the "question part" and "answer part" and therefore answer your question immediately. – Patrick Aug 21 '13 at 12:16
  • Look, I'm not gonna star my answer. I'm not here about the points, I'm here about finding an answer to my question and figuring out a way to finishing my project. If it's about the reputation, I would actually mark @btoueg's comment as his suggestion is much more elegant than mine. So, please, do not assume I answered my own question as a way of stroking myself. My project is more important than the rep points on SO. That is I have different priorities. – Alexei Aug 21 '13 at 12:21
  • 1
    I wasn't implying or assuming that you were, I just noted that you *could* include an attempted fix in your question. After all, it shows some effort in trying to solve it. I don't see why you are so quick to defend yourself, I wasn't accusing you of anything. – Patrick Aug 21 '13 at 12:27

1 Answers1

0

One way to fix it would be something like:

parseFloat(val.toFixed(2))
Alexei
  • 672
  • 1
  • 5
  • 13
  • 1
    Another way is to `var result=Math.round(val*100)/100`, which does not converts to string and back to float. Not sure if it's more efficient though. – Benjamin Toueg Aug 21 '13 at 12:12
  • @btoueg if you'd be kind enough as to post your suggestion as an answer, I would gladly mark it as "the" answer since it's obviously more elegant. – Alexei Aug 21 '13 at 12:24