Floating Point
01. What is a Floating Point?
A Floating Point type represents real numbers (numbers with fractional parts, decimals, or extreme magnitudes). Unlike integers which represent exact whole values, floating-point types represent values dynamically across a very wide range, using a scientific-notation-like representation in binary.
The term "floating point" refers to the fact that the decimal point (radix point) can "float" anywhere relative to the significant digits of the number, allowing computers to store extremely large values (like the mass of the Sun) and extremely small values (like the charge of an electron) in the same number of bits.
02. Key Characteristics
- Approximations: Floating-point types cannot represent most decimal fractions (like
0.1) exactly in binary. Consequently, computations involve minor rounding errors. - IEEE 754 Standard: Almost all modern computers utilize the IEEE 754 standard to govern floating-point calculations, specifying exact formats for storage, rounding modes, and operations.
- Special Values: Floats can represent special states including Infinity (overflow), Negative Infinity, NaN (Not a Number, representing invalid calculations like 0/0), and Signed Zero (both
+0.0and-0.0).
03. How Languages Represent It
Under the IEEE 754 standard, floating-point numbers are divided into three components:
- Exponent: Determines the magnitude (shifts the radix point).
- Mantissa / Significand: Holds the actual precision bits (significant figures).
Common formats:
- Single Precision (32-bit): Uses 1 sign bit, 8 exponent bits, and 23 mantissa bits. Offers ~7 decimal digits of precision (
floatin C/C++/Java). - Double Precision (64-bit): Uses 1 sign bit, 11 exponent bits, and 52 mantissa bits. Offers ~15-17 decimal digits of precision (
doublein C/C++/Java, defaultnumberin JavaScript,floatin Python).
04. Language Comparison
How different languages approach floating-point data types:
- Python: Python's built-in
floatis always a double-precision 64-bit float. There is no native 32-bit float primitive. - Java: Strictly defines
float(32-bit, needsfsuffix for literals) anddouble(64-bit, default for decimal literals). - JavaScript: All numerical operations are executed using 64-bit double-precision floats under the hood (the standard
numbertype). JavaScript lacks a native integers-only primitive (untilBigIntwas introduced for big integers). - C / C++: Offers
float,double, andlong double. Literals without suffixes default to double precision.
05. Code Examples
Select a tab below to see code declarations and usage:
# In Python, standard floats are double-precision (64-bit IEEE 754)
pi = 3.141592653589793
scientific = 1.2e-4 # 0.00012
print(type(pi)) # Output: <class 'float'>
print(pi) # Output: 3.141592653589793
# Floating point representation inaccuracy example:
print(0.1 + 0.2) # Output: 0.3000000000000000406. Key Differences
A comparison of key features across sizes of floating-point representations:
| Type | Size (Bits) | Significant Digits | Range (Approx.) |
|---|---|---|---|
| Single Precision (float) | 32 | ~7 decimal digits | ±1.4 × 10^-45 to ±3.4 × 10^38 |
| Double Precision (double) | 64 | ~15-17 decimal digits | ±4.9 × 10^-324 to ±1.8 × 10^308 |
07. Common Mistakes
1. Direct Equality Comparisons
Never compare two floating-point numbers directly for equality (e.g., if (a == b)). Because of binary rounding errors, calculations like 0.1 + 0.2 yield 0.30000000000000004, which is not equal to 0.3. Instead, check if the difference is smaller than a tiny tolerance value (epsilon): Math.abs(a - b) < 0.00001.
2. Financial Computations with Floats
Rounding errors accumulate. Never use float or double types for currency or exact financial arithmetic. Use integers (storing cents, e.g. 100 for $1.00) or special arbitrary-precision classes like BigDecimal in Java or decimal libraries.
3. Missing Literals Suffixes
In strongly typed languages like Java, writing float f = 3.14; will trigger a compiler error because3.14 is interpreted as a 64-bit double, which cannot be implicitly downcast to a 32-bit float. You must append an f (e.g., 3.14f).