Is there a work-around for this dividing error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is there a work-around for this dividing error?

When dividing numbers in many languages, there are some results that return an imperfect float. For example I tried to run (7 / 100) which should normally return 0.07, but actually returns something like 0.0700000001. I sort of understand how it happens; computers use approximations of numbers so division picks up a lot of noise. My question is, is there an easy way of fixing that error? Here's the calculator project I'm working on, which prompted this question: https://code.sololearn.com/Wnh1WZ81S7jb/?ref=app

20th Jul 2022, 2:26 AM
TallSkinnyHair
TallSkinnyHair - avatar
2 Answers
+ 1
The complete issue is that both the computer and humans use approximations, but we use decimal ones, while computers use binary ones. The conversion is not exact. I can think of 2 ways, not necessarily easy, to elliminate these conversion errors: 1. Use decimal notation like a*b^10, where both a and b are integers 2. Use fraction notation
20th Jul 2022, 3:38 AM
Emerson Prado
Emerson Prado - avatar
0
Knowing that you had understood the reason behind the anomaly, I guess you might also have figured that - Javascript just as any programming language relies on the same floating point processor of the machine in doing floating point arithmetics. So presumably, Javascript should also be "suffering" from similar syndrome other languages do in regards to floating point arithmetics; along with different manual workarounds each language may implement.
20th Jul 2022, 3:39 AM
Ipang