Description

In computer science, a float (short for floating-point number) is a data type used to represent real numbers—that is, numbers with fractional parts. It supports decimal points and can express both very large and very small values using scientific notation.

Floating-point numbers are essential for applications that require precision with non-integer values, such as mathematics, physics simulations, financial calculations, and graphics rendering.

Floating-Point Representation

Floats are represented according to the IEEE 754 standard, which defines how binary digits are used to encode:

  • Sign (positive or negative)
  • Exponent (scale or magnitude)
  • Mantissa / significand (precision digits)

IEEE 754 Single Precision (32-bit)

Bit SectionLengthDescription
Sign1 bit0 = positive, 1 = negative
Exponent8 bitsEncoded power of 2
Mantissa23 bitsSignificant digits (fractional)

Example:

To represent -12.5:

  • Sign = 1 (negative)
  • Exponent = 130 (biased)
  • Mantissa = 1.5625 (binary)

So in memory, the float value is stored as a combination of these parts using bit patterns.

Common Float Types in Programming

LanguageTypePrecision
C/C++float, double, long double32, 64, 80+ bits
Javafloat, double32, 64 bits
Pythonfloat (64-bit double by default)Double precision
JavaScriptNumber (only one float type)64-bit IEEE 754
C#/.NETfloat, double, decimal32, 64, 128 bits

Precision and Rounding Errors

Due to the limited number of bits, floats cannot represent all decimal values exactly, leading to rounding errors.

Example (Python):

print(0.1 + 0.2)  # Output: 0.30000000000000004

This happens because binary floating-point can’t precisely represent numbers like 0.1.

Float vs Integer

FeatureFloatInteger
ValuesReal numbers (with decimals)Whole numbers only
PrecisionLimited due to roundingExact
Use CasesScientific computation, graphicsCounters, IDs, indexing

Scientific Notation

Floats are often stored in scientific notation, especially for very large or small numbers:

1.23 × 10^4  =>  12300
6.02e23     =>  Avogadro’s number

This allows storage of large-scale numbers without exhausting memory.

Arithmetic Operations

Supported operations for floats include:

  • Addition: a + b
  • Subtraction: a - b
  • Multiplication: a * b
  • Division: a / b
  • Modulo: math.fmod(a, b) (not % for floats)
  • Power: a ** b or pow(a, b)

Caution:

Division by zero results in Infinity, -Infinity, or NaN (Not a Number), depending on the platform.

Python Example

a = 3.5
b = 2.0
print(a / b)  # Output: 1.75

You can also use the decimal or fractions module for higher precision.

JavaScript Example

let result = 0.1 + 0.2;
console.log(result);  // 0.30000000000000004

This shows that float arithmetic can be imprecise, especially in finance or critical domains.

Controlling Precision

Use formatting or arbitrary-precision libraries to control float output:

Python

print(f"{1/3:.2f}")  # 0.33

JavaScript

(1 / 3).toFixed(2);  // "0.33"

Float Comparison Pitfalls

Direct comparisons are unreliable due to precision errors:

a = 0.1 + 0.2
print(a == 0.3)  # False

Better Approach:

import math
math.isclose(a, 0.3, rel_tol=1e-9)  # True

Float Ranges

PrecisionApprox. Range
Float (32-bit)±1.4 × 10^(-45) to ±3.4 × 10^38
Double (64-bit)±4.9 × 10^(-324) to ±1.8 × 10^308
Decimal (128-bit)Smaller range, higher decimal accuracy

Applications of Floats

DomainUse Case
Graphics3D coordinates, shaders, color blending
FinanceCurrency calculations (with decimal support)
Physics EnginesMotion, collision detection
Data ScienceMachine learning, statistical models
CryptographyFloating point is generally avoided (favor integers)
Embedded SystemsOften replaced by fixed-point due to performance

Denormal Numbers, Infinity, NaN

IEEE 754 defines:

  • Denormal Numbers: Very small floats near zero
  • Infinity: Result of overflow or divide-by-zero
  • NaN: Not a Number, indicates undefined result

Python Example:

float('inf')      # Infinity
float('-inf')     # Negative Infinity
float('nan')      # Not a Number

Float vs Decimal vs Fixed-Point

FeatureFloatDecimalFixed-Point
SpeedFastSlowerFast
PrecisionApproximateExact (for base 10)Fixed
Use CaseGraphics, physicsFinanceEmbedded Systems

Security Considerations

  • Rounding bugs can cause unexpected behavior or vulnerabilities
  • Floats are avoided in cryptographic algorithms due to precision issues
  • Always validate input and output when dealing with user-provided floats

Testing Float Code

Use:

  • Tolerance-based tests (isclose, assertAlmostEqual)
  • Avoid exact matches unless values are known constants
  • Boundary tests (very small, very large, zero, negatives)

Related Terms

  • Double Precision
  • Decimal
  • Scientific Notation
  • Fixed-Point Arithmetic
  • Floating-Point Exception
  • NaN
  • Infinity
  • Truncation
  • Rounding Error
  • Bitwise Representation

Summary

A float is a critical numeric data type used to represent real numbers in computing. Although powerful and flexible, floats are inherently approximate and must be used with awareness of their limitations—particularly in high-stakes or high-precision contexts.

Whether you’re plotting 3D graphics, modeling physical simulations, or working on financial tools, understanding how floats work helps you write safer and more accurate code.