Ch 2. Internal Data Representation of Characters, Integers, Real Numbers

2.3 Real Numbers and Floating-Point Representation of Real Numbers

In computing, real numbers, those that include both rational and irrational values, are essential for representing quantities that are not whole numbers, such as measurements, scientific data, and financial values. Since computers operate using binary logic, real numbers must be approximated using a format that can handle both very large and very small values with fractional components. This is achieved through floating-point representation.

As introduced in Section 1.1, a floating-point number is typically divided into three parts: sign bit, exponent (which scales the number), and mantissa (significant digits). There are two common IEEE 754 formats: single precision (32-bit) and double precision (64-bit). Both floating-point formats use 1 bit for the sign. The single-precision format uses 8 bits for the exponent and 23 bits for the mantissa, while the double-precision format uses 11 bits for the exponent and 52 bits for the mantissa.

Example 2.8

Determine the 32-bit IEEE 754 format for the decimal number 3927.418.

For the integer part, 3927 in binary is 111101010111. For the fractional part, multiply 0.418 by 2 repeatedly and record the integer part.

0.418 x 2 = 0.836 → 0 
0.836 x 2 = 1.672 → 1 
0.672 x 2 = 1.344 → 1 
0.344 x 2 = 0.688 → 0 
0.688 x 2 = 1.376 → 1 
0.376 x 2 = 0.752 → 0 
0.752 x 2 = 1.504 → 1 
... (16 more times)

The fractional result is 0.418 ≈ 0.1101011000000100000110. The combined binary for 3927.418 is approximately 111101010111.1101011000000100000110>. Then normalize the binary by moving the decimal point to get a number between 1 and 2.

111101010111.1101011000000100000110
 = 1.1111010101111101011000000100000110 x 211

Now determine the sign bit. Since 3927.418 is positive, the sign bit = 0. Next, encode the exponent. The actual exponent is 11, the bias for 32-bit IEEE 754 is 127, so the stored exponent is 11 + 127 = 138. In binary, 138 is 10001010. To encode the mantissa, take the 23 bits after the decimal point in the normalized binary.

Mantissa = 1111010101111101011000

Finally, put the sign, exponent, and mantissa together to get the IEEE 754 representation of 3927.418 in binary

0 10001010 1111010101111101011000 

This binary representation allows the computer to store and process the value efficiently, even though it may not be exact due to rounding.

Floating-point numbers are widely used in scientific computing, engineering simulations, financial calculations, and sensor data processing (see Example 1.2 for temperature sensors). The logic depends on accurate floating-point representation to make decisions based on real-world data.

Real-World Example 2.3: Spacecraft Navigation System

Suppose you are working with a spacecraft navigating through space that needs to calculate its position, velocity, and trajectory with high precision. The spacecraft’s position in three dimensions might be represented in kilometres by

(X, Y, Z) = (7,843,219.472, -5,192,774.083, 0.0000585129)

Notice that these values involve very large and very small real numbers. To maintain accuracy over long distances, these values are stored in double-precision floating-point format (rather than single-precision).

Another reason for the high level of precision is in calculations such as for velocity, which might be calculated as distance divided by time. If the time is very small (e.g., 0.001 seconds), the division would produce a large number, requiring floating-point representation to avoid overflow or loss of precision.