How Computers Represent Fractional Numbers: The Floating-Point Standard

Integers cannot represent fractional values or the enormous range of magnitudes scientific and financial computing requires. This article explains how floating-point numbers encode a sign, exponent, and fraction into a fixed number of bits following the IEEE 754 standard, and covers the precision tradeoffs and rounding issues that come with representing real numbers this way.

Floating PointIEEE 754 StandardPrecision and Rounding

~3 دقیقه مطالعه · آخرین به‌روزرسانی ۱۵ شهریور ۱۴۰۵

Why Integers Are Not Enough

All arithmetic covered earlier in this series assumed whole numbers stored as fixed-width binary integers. Many real-world computations, however, require fractional values and numbers spanning an enormous range of magnitudes — from the mass of an electron to the distance between galaxies — which a plain integer representation cannot handle efficiently.

The Core Idea: Scientific Notation in Binary

Floating Point representation borrows the idea behind decimal scientific notation, where a number is expressed as a value multiplied by a power of a base. In binary floating point, a number is broken into three components packed into a fixed-width word:

  • Sign: a single bit indicating whether the number is positive or negative.
  • Exponent: determines the overall magnitude, effectively how far the binary point is shifted.
  • Fraction (also called the Mantissa): holds the significant digits of the number.

Conceptually, a floating-point value is interpreted as:

Value = (−1)^Sign × (1.Fraction) × 2^(Exponent − Bias)

The IEEE 754 Standard

Nearly all modern hardware, including RISC-V, follows the IEEE 754 standard for floating-point representation, which precisely defines the bit widths and behavior of these fields so that floating-point results are consistent across different processors and manufacturers.

  • Single Precision uses 32 bits total: 1 sign bit, 8 exponent bits, and 23 fraction bits.
  • Double Precision uses 64 bits total: 1 sign bit, 11 exponent bits, and 52 fraction bits, providing a much wider range and greater precision at the cost of double the storage.

The standard also uses a Bias value subtracted from the stored exponent field, which allows both very large and very small magnitudes to be represented without needing a separate sign bit for the exponent itself.

Precision Limits and Rounding

Because only a fixed number of fraction bits are available, most real numbers cannot be represented exactly and must be rounded to the nearest representable value. This has a direct practical consequence: floating-point arithmetic does not always behave like exact mathematics.

Example of a common rounding artifact:
0.1 + 0.2 does not always equal exactly 0.3
in floating-point representation, due to rounding
during binary encoding.

This is not a hardware defect but an inherent consequence of representing an infinite range of real numbers using a finite number of bits.

Special Values Defined by the Standard

IEEE 754 reserves specific bit patterns for values that do not correspond to ordinary numbers, including Positive and Negative Infinity, used to represent overflow, and NaN (Not a Number), used to represent the result of undefined operations such as zero divided by zero.

Why Understanding This Matters

Any programmer working with monetary calculations, scientific simulations, or graphics needs to understand that floating-point values carry inherent rounding error, and that comparing two floating-point numbers for exact equality is often unreliable — a direct consequence of the finite-bit representation described in this article.

نوشته و پژوهش‌شده توسط دکتر شاهین صیامی

مقالات مرتبط

Common Misconceptions About Parallel Computing and the Book's Final Lessons

After covering everything from thread-level parallelism to warehouse-scale computing, it is worth correcting persistent misconceptions about parallel systems that even experienced engineers sometimes hold. This article addresses common fallacies about scaling and parallel hardware, then closes out the parallel processing chapter by tying together the full journey from a single instruction to a building full of cooperating machines.

ادامه

Real Stuff: Benchmarking CPUs Against GPUs and Multiprocessor Matrix Multiply

Comparing a CPU and a GPU fairly requires a model that accounts for both computational throughput and memory bandwidth limits together. This article introduces the roofline model used to compare real hardware like the Intel Core i7 and NVIDIA Tesla GPU, then shows how matrix multiplication is accelerated across multiple processors as the final practical application of this chapter's parallel concepts.

ادامه

Benchmarking Multiprocessors and Modeling Parallel Performance

Measuring the performance of a parallel system requires different tools and metrics than measuring a single-core processor. This article covers the specialized benchmarks used to evaluate multiprocessor systems, explains how to model scaling behavior as more processors are added, and revisits Amdahl's Law in the context of real-world performance measurement.

ادامه

Cluster Networking: Connecting to the World Outside

A cluster of machines is only useful if it can communicate efficiently both internally and with the outside world. This article covers the networking layers involved in cluster communication, the tradeoffs between latency and bandwidth at scale, and how clusters connect to external networks and users.

ادامه

Clusters, Warehouse-Scale Computers, and Network Topologies

Beyond a single chip, parallelism extends to entire buildings full of independent computers working together. This article explains the shift from shared memory multiprocessing to clusters of separate machines, introduces the concept of warehouse-scale computing, and covers the network topologies that connect these independent machines efficiently.

ادامه

An Introduction to GPUs: Massive Parallelism for Data-Heavy Workloads

A GPU takes the SIMD idea covered earlier in this series to an extreme scale, running thousands of lightweight threads simultaneously to process massive amounts of independent data. This article explains why GPUs are architecturally so different from CPUs, how their thread execution model works, and what kinds of workloads benefit most from this design.

ادامه