Integer vs Floating Point Arithmetic

See how computers handle different types of numbers!

Click to Try Examples:

🔢 Integer Arithmetic

and
🔲 Computer sees:

How it works:

Integers are stored as exact binary numbers. Each digit is a power of 2.

Example: 10 = 8+2 = 2³+2¹ = 00001010

ℹ️ Integers are whole numbers. Division throws away decimals!

🔢. Floating Point Arithmetic

and
🔲 Computer sees:

Why the error?

0.1 and 0.2 can't be represented exactly in binary!

It's like trying to write 1/3 in decimal: 0.3333...

How it works:

Floats use scientific notation in binary.

They store: sign, exponent, and fraction

ℹ️ Floats can have decimals but may have tiny precision errors.

💡 Key Differences

Integers

  • • Exact values (no decimals)
  • • Stored as simple binary numbers
  • • Division drops the decimal part
  • • Perfect for counting things
  • • Limited range (e.g., -2³¹ to 2³¹-1)

Floating Point

  • • Can have decimal values
  • • Stored in scientific notation
  • • Small precision errors possible
  • • Good for measurements
  • • Some decimals can't be exact!

💻 Computer Storage Comparison

Integer (e.g., 42):

00101010

Direct binary representation

Float (e.g., 42.0):

01000010 00101000 00000000 00000000

Sign (1 bit) + Exponent (8 bits) + Fraction (23 bits)

🎯 When to Use What?

Use Integers for:

  • ✓ Counting (loop counters, array indices)
  • ✓ IDs and keys
  • ✓ Exact calculations (money in cents)
  • ✓ When you don't need decimals

Use Floats for:

  • ✓ Scientific calculations
  • ✓ Measurements (height, weight)
  • ✓ Percentages and ratios
  • ✓ Graphics and physics