1 // polynomial for approximating log(1+x)
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 deg = 12; // poly degree
8 // |log(1+x)| > 0x1p-4 outside the interval
12 // find log(1+x)/x polynomial with minimal relative error
13 // (minimal relative error polynomial for log(1+x) is the same * x)
14 deg = deg-1; // because of /x
16 // f = log(1+x)/x; using taylor series
18 for i from 0 to 60 do { f = f + (-x)^i/(i+1); };
20 // return p that minimizes |f(x) - poly(x) - x^d*p(x)|/|f(x)|
21 approx = proc(poly,d) {
22 return remez(1 - poly(x)/f(x), deg-d, [a;b], x^d/f(x), 1e-10);
25 // first coeff is fixed, iteratively find optimal double prec coeffs
27 for i from 1 to deg do {
28 p = roundcoefficients(approx(poly,i), [|D ...|]);
29 poly = poly + x^i*coeff(p,0);
32 display = hexadecimal;
33 print("rel error:", accurateinfnorm(1-poly(x)/f(x), [a;b], 30));
34 print("in [",a,b,"]");
36 for i from 0 to deg do coeff(poly,i);