TL;DR
When implementing mathematical functions in software, we usually approximate them with polynomials. While finding the best "theoretical" polynomial is easy, finding the best one whose coefficients actually fit into floating-point formats is an NP-hard problem equivalent to finding the closest point in a high-dimensional lattice. This paper shows that despite this theoretical complexity, we can use lattice reduction algorithms to find near-optimal floating-point approximations in less than a second.
What Problem Does This Paper Address?
In computer arithmetic, we often need to evaluate functions like $\sin(x)$ or $\exp(x)$ quickly and accurately. The standard approach is to use a polynomial approximation. Mathematically, finding the "best" polynomial (the one that minimizes the distance to the target function) is a solved problem. If you want to minimize the average squared error ($L^2$ norm), you just project the function onto a vector space. If you want to minimize the maximum error ($L^\infty$ norm), you use the Remez algorithm.
However, there’s a catch: these "best" polynomials have coefficients that are arbitrary real numbers. Computers don't store arbitrary real numbers; they store floating-point numbers (machine numbers).
The "naive" solution is to find the best theoretical polynomial and then round its coefficients to the nearest floating-point values. Unfortunately, this often leads to a massive loss in accuracy. The authors tackle the Floating-Point $L^2$-Approximation problem: how do we find the best possible polynomial where the coefficients are constrained to be machine numbers from the start?
Methods
The authors transform the problem of finding optimal coefficients into a geometric problem involving lattices.
- Mathematical Formulation: They show that minimizing the $L^2$ distance between a function $f$ and a polynomial with floating-point coefficients is equivalent to finding a vector in a discrete lattice that is closest to a specific target point.
- Complexity Analysis: They prove that this problem is NP-hard by reducing it to the Closest Vector Problem (CVP). This means that, in theory, as the degree of the polynomial grows, the problem becomes exponentially difficult to solve perfectly.
- Algorithmic Solutions:
- Preprocessing: They use the LLL (Lenstra-Lenstra-Lovász) algorithm to "reduce" the lattice basis, making it more orthogonal and easier to search.
- Babai’s Nearest Plane Algorithm: An efficient (but approximate) method to find a "good enough" vector in the lattice.
- Kannan’s Method: An exact method that searches within an ellipsoid to find the absolute best vector.
- Heuristics for Precision: They provide two heuristics to help developers choose the right bit-precision for each coefficient, ensuring that the search space is optimized without sacrificing accuracy.
Datasets
As a theoretical and algorithmic paper, it does not use large-scale training datasets. Instead, it evaluates the proposed methods on specific mathematical functions commonly used in library development:
- Example 1: The function $x \mapsto \sin(\pi\sqrt{x})/(\pi\sqrt{x})$ on the interval $[0, 1]$.
- Example 2: The $\arctan$ function on the interval $[-1, 1]$.
These serve as benchmarks to compare the $L^2$ approach against traditional "rounded minimax" ($L^\infty$) approaches.
Key Insights and Ideas
- The $L^2$ Advantage: While $L^\infty$ (minimax) is usually the "gold standard" for function libraries because it guarantees a maximum error bound, $L^2$ is mathematically "nicer." Because $L^2$ is based on inner products (a Hilbert space), it maps directly to the geometry of lattices, allowing the use of powerful tools from number theory.
- The Rounding Gap: The paper highlights just how bad naive rounding is. By searching the lattice instead of just rounding, they achieved a 10,000x improvement in $L^2$ error for a degree-8 polynomial.
- NP-Hard but Practical: Even though the problem is NP-hard, the "dimensions" involved in function approximation (usually polynomial degrees under 50) are small enough that modern lattice algorithms can solve them almost instantly.
- Bridge to $L^\infty$: A key insight is that a best $L^2$ approximation is often very close to the best $L^\infty$ approximation. This makes the $L^2$ method an excellent "warm start" or preprocessing step for more expensive $L^\infty$ searches.
Key Results
- Efficiency: All tested approximations, even for higher-degree polynomials, took less than one second on a 2GHz Pentium M processor.
- Accuracy Gains: In the $\sin(\pi\sqrt{x})/(\pi\sqrt{x})$ example, the lattice-based $L^2$ approach produced an absolute error 100 times smaller than the standard rounded minimax approach.
- Relative Error: The method is versatile enough to handle relative error (minimizing percentage error rather than absolute distance), which is crucial for functions with large variations in magnitude. In the $\arctan$ example, they improved the relative error by a factor of 5 over traditional methods.
Limitations and Future Work
- Numerical Stability: The authors note that implementations of the LLL algorithm can sometimes face numerical instability on certain types of matrices, though this didn't impact their specific applications.
- Dependency on $x$: The quality of the approximation can depend on the specific value of $x$ being computed, though they suggest theoretical ways to bound this dependency.
- Future Direction: The authors propose using the $L^2$ result as a starting point to "list" all nearby vectors. By checking the $L^\infty$ norm of these candidates, one could potentially find the absolute best $L^\infty$ floating-point approximation more efficiently than current methods.
- Multivariate Functions: They briefly mention that this method can be directly adapted to approximate functions with two or more variables.
Summary updated