Egghead.page Logo

Sinclair ZX Spectrum BASIC Floating-Point Storage Format

The Sinclair ZX Spectrum utilized a compact five-byte structure to handle floating-point arithmetic within its BASIC interpreter. This article examines the specific memory allocation for these numbers, detailing the separation of exponents and mantissas. It also discusses the resulting precision and the hardware constraints that influenced this engineering decision.

When the ZX Spectrum was released in 1982, memory was an extremely scarce resource. With base models offering only 16KB of RAM, the engineers at Sinclair Research had to design a numerical system that balanced range and precision with minimal memory footprint. Unlike modern systems that often use the IEEE 754 standard requiring four or eight bytes per number, Sinclair BASIC implemented a proprietary 40-bit floating-point format. This format consumed exactly five bytes of memory for every non-integer variable, allowing the system to manage a wide range of values without exhausting the available RAM.

The storage structure is divided into two distinct parts: the exponent and the mantissa. The first byte is dedicated entirely to the exponent, which determines the scale of the number. This exponent is stored with a bias of 128, meaning that the actual exponent value is calculated by subtracting 128 from the stored byte value. This allows the system to represent both very large and very small numbers by shifting the decimal point accordingly. The remaining four bytes are allocated to the mantissa, which holds the significant digits of the number.

Precision and sign handling are managed within the mantissa section. The most significant bit of the first mantissa byte serves as the sign bit, indicating whether the number is positive or negative. The remaining 31 bits store the fractional data. This configuration provides approximately eight to nine decimal digits of precision. While this was sufficient for most educational and gaming applications of the era, it occasionally led to rounding errors in complex mathematical calculations, a characteristic quirk remembered by many original users.

This custom floating-point implementation was a direct response to the limitations of the Z80 processor and the tight memory constraints of the hardware. By avoiding the overhead of more complex standards, Sinclair ensured that BASIC programs could run efficiently on modest hardware. Although obsolete by modern computing standards, this five-byte format remains a fascinating example of efficient low-level programming and resource management in the early home computer era.