The Right Way to Compare Floats in Python | by David Amos

  1. The number has more significant digits than floating points allow.
  2. The number is irrational.
  3. The number is rational but has a non-terminating binary representation.

The Decimal Type

The Fraction Type

  • Why floating-point numbers are imprecise
  • Why floating-point representation error is common
  • How to correctly compare floating-point values ​​in Python
  • How to represent numbers exactly using Python’s Fraction and Decimal types

Leave a Comment