Egghead.page Logo

How the Sinclair ZX81 Calculated Sine and Cosine

The Sinclair ZX81, a pioneering home computer from 1981, managed to perform complex mathematical operations despite severe hardware limitations. This article explores the software algorithms embedded in the ZX81’s ROM that allowed it to compute trigonometric functions like sine and cosine without a dedicated math coprocessor. We will examine the floating-point format used, the technique of range reduction, and the polynomial approximations that enabled accurate calculations within just 1KB of RAM.

Hardware Constraints and Design Philosophy

To understand how the ZX81 handled trigonometry, one must first appreciate the constraints under which it operated. The machine was powered by a Z80 CPU clocked at 3.25 MHz and lacked a floating-point unit (FPU). All mathematical operations had to be performed via integer arithmetic manipulated by software routines stored in the 8KB read-only memory (ROM). With a base model containing only 1KB of random-access memory (RAM), efficiency was not just a goal but a necessity. Sinclair prioritized cost reduction, meaning the mathematical precision had to be balanced against the available processing power and memory space.

The Sinclair Floating-Point Format

Unlike modern systems that adhere to the IEEE 754 standard, the ZX81 utilized a proprietary 5-byte floating-point format. This format consisted of one byte for the exponent and four bytes for the mantissa. This structure allowed the computer to represent a wide range of numbers while conserving memory. When a user requested a sine or cosine calculation in Sinclair BASIC, the input value was converted into this 5-byte format. The mathematical routines then operated on these bytes directly, using shift and add operations to simulate multiplication and division required for higher-level math.

Range Reduction Techniques

The core challenge in calculating trigonometric functions is that sine and cosine are periodic, repeating every 2π radians. Calculating a series expansion for a large number would be computationally expensive and prone to error. To solve this, the ZX81 ROM employed a technique known as range reduction. The software would take any input angle and mathematically reduce it to a primary interval, typically between 0 and π/2. By mapping any possible angle to this smaller quadrant, the computer could use a single, optimized approximation routine for all inputs, significantly speeding up the process.

Polynomial Approximation Algorithms

Once the angle was reduced to the primary interval, the ZX81 calculated the function value using polynomial approximation. Instead of calculating infinite series, the ROM contained specific coefficients for polynomials that closely matched the curve of the sine or cosine function within the reduced range. These coefficients were carefully chosen to minimize error across the interval. The Z80 processor would evaluate this polynomial using Horner’s method, which minimizes the number of multiplications required. This approach allowed the ZX81 to return results accurate to several decimal places in a fraction of a second, a remarkable feat for its time.

Legacy of the ZX81 Math Package

The mathematical routines written for the ZX81 and its predecessor, the ZX80, were highly regarded for their efficiency. They demonstrated that sophisticated mathematical capabilities could be achieved on low-cost hardware through clever software engineering. While the precision was not suitable for high-end scientific research, it was more than adequate for education, simple graphics, and gaming. The methods used in the ZX81 paved the way for future home computers, proving that complex trigonometric functions could be accessible to the general public without expensive dedicated hardware.