educative.io

Multiplication with decimals

10.2 * 3 is supposed to be 30.6 but it says another answer which is 30.599999999999998 May i know why is that or is there a bug

This is a limitation of Python, and computer hardware in general. Machines use a binary representation to represent base 10 (normal) numbers. However, decimals cannot be fully captured in the binary system. This is because some decimal numbers require a binary representation of infinite length. Hence, Python makes an approximation of what the number actually is.

Even in the fractions that Python displays as complete (e.g., 0.1), it is actually omitting the estimation (e.g., 0.100000000024864) for our ease.

4 Likes