 0.1+0.2 === 0.3 // output: false | SoloLearn: Learn to code for FREE!

+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?

9 Answers

New Answer

+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.

+6

other oddities: null is an object, NaN is a number, array without keys == false //Answer: true

+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

+1

Vansh Kaushik please can you create different threads to ask your question So no one would get confused

+1

The accuracy of calculating is so bad for decimal number, I also have the same problem when working on my python calculator

0

How i make program if i write hello he say hello vansh

0

0

When i am running my calculator on JS, I have the same problem.