0

I was using javascript to do some "ratio conversion" on up to two decimal digits numbers (I mean operations like 0.nn => nn% e.g. from 0.21 to 21%) , when I noticed that if I do an operation like 0.56 * 100 I don't get 56 as I'd expect but get an unexpected decimal part togheter (something like 56.000...0001). I noticed this happens with any ((7*2^x)/100) * y values (having y != 2^n number). I experienced this in different platform (desktop and mobile) and different browsers (Firefox, Chrome, IE, Safari) Can someone explain me why does this happen?

danicotra
  • 1,333
  • 2
  • 15
  • 34
  • 1
    How many times a week will a floating point "bug" question be posted? – rmlan Mar 14 '18 at 15:21
  • [How to deal with floating point number precision in JavaScript?](https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript) – Alex K. Mar 14 '18 at 15:21
  • 2
    Please do read the excerpt of the tags you add. Even better [read the tag info pages of them](https://stackoverflow.com/tags/floating-point/info). They are a treasure trove of good information (sometimes). – Heretic Monkey Mar 14 '18 at 15:22
  • @rmlan: If you think duplicate floating-point questions are entered too frequently, you could take action to reduce them. For starters, I suggest entering a prepared Q&A on the subject “How does floating-point arithmetic work?” That would be more attractive to questioners than “Is floating point math broken?” Additionally, prepared Q&As can be entered on specific issues that are asked about repeatedly, such as what values are representable, what operations are exact, how rounding errors can be reduced, what alternatives there are, and so on. – Eric Postpischil Mar 14 '18 at 17:28
  • One Q&A that could be useful is “What floating-point system do languages use?”, with an answer listing that Java specifies IEEE 754, Python allows the host system’s floating-point to be inherited, along with finer details about what the language standards say, such as whether operations are strictly IEEE 754, whether excess precision may be used, whether expressions may be contracted, and so on. – Eric Postpischil Mar 14 '18 at 17:30
  • Another useful Q&A could explain how various languages (and specific compilers, where suitable) format floating-point outputs by default and what options they offer. Some default to a fixed number of digits. Some use the fewest decimal digits that uniquely distinguish the floating-point value. Some use enough decimal digits to uniquely distinguish the value but not necessarily the fewest. Some offer the ability to print the value exactly, even if it requires many digits. – Eric Postpischil Mar 14 '18 at 17:32
  • Sure, but from personal experience, the majority of questions that I come across that end up being marked as a dupe for the floating point question mention _nothing_ about floating point. I think it is a terminology issue. – rmlan Mar 14 '18 at 17:35

0 Answers0