0.1+0.2 === 0.3 // output: false | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

0.1+0.2 === 0.3 // output: false

Very interesting. I tried on JavaScript but something went wrong. 0.1+0.2=0.30000000004 what? really? I so confused. Why result show me wrong answer?

17th May 2021, 1:00 AM
Chingun Undrakh
Chingun Undrakh - avatar
9 Answers
+ 18
This is an action of the accuracy of the calculations. When JavaScript performs calculations, it converts the values to binary equivalents. And here there is a problem, since 0.1 can not be exactly represented in the binary equivalent, which is used for binary floating-point operations. In order to overcome this incident, there are two methods (the choice is yours): 1. Convert values to integers, calculate, and then reverse convert to decimals. 2. building the logic so that it works with a range of numbers, not an exact value.
17th May 2021, 3:50 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 6
other oddities: null is an object, NaN is a number, array without keys == false //Answer: true
17th May 2021, 3:56 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 3
floating point arithmetic is not always 100% accurate. To solve this type of problem you can use (0.1*10 + 0.2*10) / 10
23rd May 2021, 6:17 PM
Prafull Epili
Prafull Epili - avatar
+ 1
Vansh Kaushik please can you create different threads to ask your question So no one would get confused
18th May 2021, 3:31 PM
Ananiya Jemberu
Ananiya Jemberu - avatar
+ 1
The accuracy of calculating is so bad for decimal number, I also have the same problem when working on my python calculator
18th May 2021, 4:06 PM
Ngoc Tuan
Ngoc Tuan - avatar
0
How i make program if i write hello he say hello vansh
18th May 2021, 5:06 AM
Vansh Kaushik
Vansh Kaushik - avatar
0
Vansh Kaushik 🤔🤔🤔🤔🤔
18th May 2021, 5:09 AM
Prafull Epili
Prafull Epili - avatar
0
When i am running my calculator on JS, I have the same problem.
18th May 2021, 6:48 AM
Kind_Cat
Kind_Cat - avatar
0
Buena
18th May 2021, 9:49 PM
Astton Baltazar