Faster math algorithm sacrificing accuracy
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Many performance-critical applications deliberately trade mathematical precision for speed. Graphics engines, real-time physics simulations, machine learning inference, and high-frequency trading all use approximations that are "close enough" while running orders of magnitude faster than exact methods. This article covers several well-known techniques, explains why they work, and shows when the accuracy loss is acceptable.
Fast Inverse Square Root
The most famous example of sacrificing accuracy for speed is the fast inverse square root, popularized by the Quake III Arena source code. Computing 1 / sqrt(x) normally requires a division and a square root, both expensive operations. The fast version uses a bit-level trick on floating point representation to produce a good initial guess, then refines it with one iteration of Newton's method.
This produces results within about 1% of the true value, which is more than sufficient for lighting calculations and surface normals in 3D rendering. Modern CPUs have dedicated rsqrtss instructions that use the same principle in hardware.
Lookup Tables with Interpolation
Trigonometric functions like sin() and cos() are expensive to compute exactly. A common alternative is to precompute values at fixed intervals and interpolate between them at runtime.
With 1,024 entries, linear interpolation gives errors under 0.001 for most inputs. Increasing the table size to 4,096 entries pushes errors below 0.00001. Audio DSP systems, game physics, and embedded signal processing routinely use this technique.
Single Precision vs Double Precision
Switching from 64-bit double precision to 32-bit single precision floats halves memory usage and can double throughput on modern GPUs and SIMD units. Single precision provides about 7 decimal digits of precision, while double provides about 15.
Machine learning training increasingly uses mixed precision (float16 for forward pass, float32 for gradients) to gain speed with minimal accuracy impact. NVIDIA's Tensor Cores are specifically designed for this pattern.
Approximate Counting and Sketches
When exact counts are not needed, probabilistic data structures provide dramatic speedups and memory reductions.
HyperLogLog estimates the cardinality (number of distinct elements) of a set using only about 12 KB of memory regardless of set size, compared to storing all elements which could require gigabytes. The error rate is typically around 2%.
Redis, BigQuery, and most analytics platforms use HyperLogLog to count unique visitors, distinct IPs, and unique queries without storing every value.
Stochastic Rounding
Standard rounding always rounds 0.5 up (or to even), introducing a systematic bias. Stochastic rounding randomly rounds up or down with probability proportional to the fractional part, eliminating bias in aggregate while introducing per-element noise.
This technique is used in low-precision neural network training (BFloat16, INT8) where biased rounding would cause gradients to drift. The errors cancel out over many operations, preserving training convergence.
When Approximation Is Acceptable
The right trade-off depends on the domain:
- Games and graphics: Errors under 1% are invisible to the human eye. Speed matters far more than precision for 60fps rendering.
- Machine learning inference: Model weights are already approximate. Quantizing from float32 to int8 typically loses less than 1% accuracy while running 2-4x faster.
- Analytics and counting: Knowing that a page had "about 1.2 million" unique visitors is just as useful as the exact count for most business decisions.
- Scientific computing: Exact precision matters. Double precision or higher is usually required, and approximation shortcuts are rarely acceptable.
- Financial calculations: Rounding errors in currency amounts can cause legal and accounting problems. Use exact decimal arithmetic, not floating point.
Common Pitfalls
- Applying approximation techniques in domains where exact answers are required, like financial calculations or cryptographic operations.
- Using single precision for iterative algorithms that amplify rounding errors over many steps. Small per-step errors can compound into large final errors.
- Assuming that a lookup table is always faster. On modern CPUs with fast math units, the cache miss from a large table can be slower than computing the function directly.
- Not measuring the actual error bounds of your approximation. "Close enough" should be quantified, not assumed.
- Mixing precision levels without understanding where conversions happen. Implicit float-to-double promotions can silently change performance characteristics.
Summary
Faster math through approximation is a deliberate engineering trade-off, not a compromise. Techniques like fast inverse square root, lookup tables, reduced precision arithmetic, and probabilistic counting are standard tools in performance-sensitive systems. The key is knowing your error tolerance, measuring the actual error introduced, and choosing the right technique for your domain.

