educative.io

Question about Modulo

Hi,
why the output of print(34.4 % 2.5) is 1.8999999999999986


Course: https://www.educative.io/courses/python-fundamentals-for-programmers
Lesson: https://www.educative.io/courses/python-fundamentals-for-programmers/gxxLA4WEoD3

Hi @Yanchen_Shen,

The output of print(34.4 % 2.5) is 1.8999999999999986 due to the way floating-point arithmetic is handled in Python.

Floating-point arithmetic in computers can sometimes lead to rounding errors due to the limitations of representing real numbers in binary format. Therefore, when performing calculations involving floating-point numbers, you may encounter small discrepancies in the results.

In this case, 34.4 % 2.5 should mathematically result in 1.9. However, due to the aforementioned floating-point arithmetic limitations, the actual result may be slightly different. This discrepancy is commonly observed in computer programming languages that use floating-point arithmetic.

If precise calculations are required, it’s recommended to use decimal arithmetic or appropriate rounding techniques to handle floating-point numbers.

I hope this helps!!
Happy Learning :slight_smile:

1 Like