Andromeda
Note

Floats (Python)

Definition

Floats (float), or floating-point numbers, in Python are numbers that contain a decimal point.

Why It Matters

Floats are used for measurement and approximation of continuous real-world values. However, failing to understand how they work can lead to “logic bombs” in high-stakes software, as precision loss and rounding errors accumulate.

Core Concepts

# Standard floats
x = 0.1
y = 0.2
print(x + y)  # 0.30000000000000004 (Precision limit)

# Mixed operations
result = 42 + 0.5
print(type(result))  # <class 'float'>
  • Precision Limits: Computers represent floats using base-2 fractions, which can lead to tiny inaccuracies in representation.
  • Mixed Operations: Operations involving both an int and a float will always result in a float.
  • Decimal Module: For cases like financial calculations where exact decimal representation is needed, Python provides specialized modules like decimal instead of standard floats.

Connected Concepts