🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Floats

AI & DATA SCIENCE // floats

A float is Python's built-in type for numbers with a fractional part, implemented as an IEEE-754 double-precision binary number.

Syntax

x = 3.14
y = -0.5
z = 1.5e3  # scientific notation
inf = float("inf")

Deep Dive Course

Every float in Python occupies 64 bits and follows the IEEE-754 standard, the same format used in most programming languages, giving about 15-17 significant decimal digits of precision. Because it's binary rather than decimal, many ordinary decimal fractions, like 0.1, can't be stored exactly, which is why float arithmetic accumulates small rounding errors. Special values inf, -inf, and nan represent infinity and 'not a number' results, such as from dividing by zero with floats or taking the square root of a negative number.

1Understanding Floats

Every float in Python occupies 64 bits and follows the IEEE-754 standard, the same format used in most programming languages, giving about 15-17 significant decimal digits of precision. Because it's binary rather than decimal, many ordinary decimal fractions, like 0.1, can't be stored exactly, which is why float arithmetic accumulates small rounding errors. Special values inf, -inf, and nan represent infinity and 'not a number' results, such as from dividing by zero with floats or taking the square root of a negative number.

💡

Never use == to compare two computed floats — use math.isclose() instead, since rounding error means two mathematically equal expressions can differ in their last binary digits.

editor.html
print(0.1 + 0.2)
print(1.5e3)
print(float("inf") > 10 ** 100)
localhost:3000

2Practical Example

Here is a real-world application of Floats showing how it is used in production Python code.

editor.html
import math
a = 0.1 + 0.2
b = 0.3
print(a == b)
print(math.isclose(a, b))
localhost:3000

3Best Practices

Follow these guidelines when working with Floats:

1. Use the decimal module instead of float for money or anywhere exact decimal arithmetic matters

2. Use math.isclose(a, b) rather than == for float comparisons

3. Check for nan with math.isnan(x) instead of comparing to float('nan') directly, since nan is never equal to anything, including itself

⚠️

Tip: Never use == to compare two computed floats — use math.isclose() instead, since rounding error means two mathematically equal expressions can differ in their last binary digits.

editor.html
print(0.1 + 0.2)
print(1.5e3)
print(float("inf") > 10 ** 100)
localhost:3000

Examples

Example 01Basic Usage
print(0.1 + 0.2)
print(1.5e3)
print(float("inf") > 10 ** 100)
Example 02Advanced Example
import math
a = 0.1 + 0.2
b = 0.3
print(a == b)
print(math.isclose(a, b))

Best Practices

  • Use the decimal module instead of float for money or anywhere exact decimal arithmetic matters
  • Use math.isclose(a, b) rather than == for float comparisons
  • Check for nan with math.isnan(x) instead of comparing to float('nan') directly, since nan is never equal to anything, including itself

Interview Question

Why is float('nan') never equal to itself, and how do you actually check for it?

Hint: nan stands for 'not a number' and follows a specific IEEE-754 rule.

The IEEE-754 standard specifically defines nan as unequal to every value, including another nan, so that any computation that produces an invalid result reliably fails an equality check rather than silently comparing equal to something. To test for it, use math.isnan(x), which checks the bit pattern directly instead of relying on equality.

Exercises

MediumPractice using Floats in a real scenario.
View Solution
print(0.1 + 0.2)
print(1.5e3)
print(float("inf") > 10 ** 100)

Frequently Asked Questions

Why is float('nan') never equal to itself, and how do you actually check for it?

The IEEE-754 standard specifically defines nan as unequal to every value, including another nan, so that any computation that produces an invalid result reliably fails an equality check rather than silently comparing equal to something. To test for it, use math.isnan(x), which checks the bit pattern directly instead of relying on equality.

Related Functions

integersdecimal.Decimalmath.isclose()