Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 29, 2022 01:51 pm GMT

Why 0.10.2==0.3 False?

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
  • 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
  • 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
  • Now you understand why it returned false.
console.log(0.1 + 0.2 === 0.30000000000000004)//output - true
  • This time it will return true

THANK YOU FOR CHECKING THIS POST

You can contact me on -
Instagram - https://www.instagram.com/s_h.u_b.h_a.m_2k99/
LinkedIn - https://www.linkedin.com/in/shubham-tiwari-b7544b193/
Email - [email protected]

^^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


Original Link: https://dev.to/shubhamtiwari909/why-010203-false-ifd

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To