Why 0.1+0.2==0.3 False?

Shubham Tiwari - Aug 29 '22 - - Dev Community

Hello Everyone today i will be discussing this famous question
Why 0.1 + 0.2 === 0.3 is false?

Let's get started...

I am using Javascript to show this but this comparison of floating point can be seen in other programming languages as well where they also return false for this comparison.

Code Example -

console.log(0.1 + 0.2 === 0.3)
//output - false
Enter fullscreen mode Exit fullscreen mode
  • It returned false but 0.1 + 0.2 = 0.3 then it why it didn't returned true?
  • Well These floating point numbers cannot be precisely represented in Base 2 floating point number.This Explaination of base 2 concept is quite complicated to show but i will show you the simple reason why it happened.
console.log(0.1 + 0.2)
// output - 0.30000000000000004
Enter fullscreen mode Exit fullscreen mode
  • The actual output of those 2 numbers is 0.30000000000000004 that's why it returned false because we are comparing this
console.log(0.3 === 30000000000000004)
// false
Enter fullscreen mode Exit fullscreen mode
  • Now you understand why it returned false.
console.log(0.1 + 0.2 === 0.30000000000000004)
//output - true
Enter fullscreen mode Exit fullscreen mode
  • This time it will return true

THANK YOU FOR CHECKING THIS POST ❤❤

You can contact me on -
Instagram -https://www.instagram.com/supremacism__shubh/
LinkedIn - https://www.linkedin.com/in/shubham-tiwari-b7544b193/
Email - shubhmtiwri00@gmail.com

^^You can help me by some donation at the link below Thank you👇👇 ^^
☕ --> https://www.buymeacoffee.com/waaduheck <--

Also check these posts as well
https://dev.to/shubhamtiwari909/js-push-and-pop-with-arrays-33a2/edit

https://dev.to/shubhamtiwari909/tostring-in-js-27b

https://dev.to/shubhamtiwari909/join-in-javascript-4050

https://dev.to/shubhamtiwari909/going-deep-in-array-sort-js-2n90

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .