From "Code"
🎧 Listen to Summary
Free 10-min PreviewRepresentation of Non-Integer Numbers in Computers
Key Insight
Computers inherently operate on discrete binary values, making the representation of continuous numbers, particularly fractions, complex, unlike human comfort with decimals and percentages. While whole numbers (integers) are stored using binary and two's complement for negatives (e.g., 32-bit positive integers range from 0 through 4294967295, two's complement from -2147483648 through 2147483647), fractional values, categorized mathematically as rational (ratios of integers like 0.75 or 1/3 repeating) or irrational (non-repeating, non-terminating decimals like pi), require specialized methods. One initial approach is Binary-Coded Decimal (BCD), where each decimal digit (0-9) is encoded with 4 bits. Packed BCD stores two BCD digits per byte and utilizes an additional sign bit to denote positive or negative values, often sacrificing 4 or 8 bits for it.
BCD is particularly effective for fixed-point formats, where the decimal point's position is implicitly known by the program, but not stored. This method excels in applications requiring consistent decimal precision, such as financial calculations (e.g., dollars and cents), where amounts like -4325120.25 can be represented using 5 bytes (1 sign nibble, 19 BCD digits, for a range of –9999999.99 through 9999999.99). However, fixed-point format critically fails when dealing with numbers of vastly different magnitudes, such as the distance from Earth to the Sun (490000000000 feet) and the radius of a hydrogen atom (0.00000000026 feet), which would necessitate an impractical 12 bytes of storage for a single number. This limitation highlights the need for a more flexible representation, leading to the adoption of scientific notation principles.
Floating-point notation, based on binary scientific notation where numbers are expressed as a significand multiplied by a power of two (e.g., 101.1101 binary is 1.011101 x 2^2), addresses this range issue. The IEEE Standard 754-1985 defines formats like single precision (4 bytes/32 bits) and double precision (8 bytes/64 bits). Single precision uses 1 sign bit, 8 exponent bits (with a bias of 127), and 23 significand fraction bits (implying a leading '1', providing 24-bit precision). This allows representation of numbers from approximately 1.175494351 x 10^-38 to 3.402823466 x 10^38. Double precision extends this with an 11-bit exponent (bias 1023) and 52 significand fraction bits (53-bit precision), covering a range from approximately 2.2250738585072014 x 10^-308 to 1.7976931348623158 x 10^308. While floating-point offers vast range, its limited precision means that distinct decimal numbers, such as 16777216 and 16777217 in single precision (both stored as 4B800000h), or $262144.00 and $262144.01, can be stored as identical binary values, making it unsuitable for applications requiring exact decimal precision, like banking. It can also lead to minor calculation inaccuracies (e.g., 3.499999999999 instead of 3.50). Special values like zero, infinities, and 'Not a Number' (NaN) are also defined within the IEEE standard.
📚 Continue Your Learning Journey — No Payment Required
Access the complete Code summary with audio narration, key takeaways, and actionable insights from Charles Petzold.