-1

Why doesn't 0.1+0.2-0.3 equal 0.0 ? according to normal equation the result should be 0 but why isn't it?

Could someone please explain me the reason?

matszwecja
  • 6,357
  • 2
  • 10
  • 17

1 Answers1

-1

All programming languages including Python use 32-bit floating point representation for numbers. With decimal fractions, this floating-point number system causes some rounding errors which results in 0.1 + 0.2 == 0.3 as false.

The Actual Answer

For Example:

1.2 is exactly equal to 1.1999999999999999555910790149937383830547332763671875

Solution:

One solution to handle this inconsistency in floating-point value is by using a native Python module called Decimal


For Detailed Answer, Read these articles:

https://gauravkk22.medium.com/why-0-1-0-2-0-3-is-false-in-js-mystery-unsolved-with-solution-4f7db2755f18

https://python.plainenglish.io/floating-point-arithmetic-precision-error-in-python-decimal-comes-for-rescue-8803d1290601https://python.plainenglish.io/floating-point-arithmetic-precision-error-in-python-decimal-comes-for-rescue-8803d1290601