1// polynomial for approximating log(1+x) 2// 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 6 7deg = 12; // poly degree 8// |log(1+x)| > 0x1p-4 outside the interval 9a = -0x1p-4; 10b = 0x1.09p-4; 11 12// find log(1+x)/x polynomial with minimal relative error 13// (minimal relative error polynomial for log(1+x) is the same * x) 14deg = deg-1; // because of /x 15 16// f = log(1+x)/x; using taylor series 17f = 0; 18for i from 0 to 60 do { f = f + (-x)^i/(i+1); }; 19 20// return p that minimizes |f(x) - poly(x) - x^d*p(x)|/|f(x)| 21approx = proc(poly,d) { 22 return remez(1 - poly(x)/f(x), deg-d, [a;b], x^d/f(x), 1e-10); 23}; 24 25// first coeff is fixed, iteratively find optimal double prec coeffs 26poly = 1; 27for i from 1 to deg do { 28 p = roundcoefficients(approx(poly,i), [|D ...|]); 29 poly = poly + x^i*coeff(p,0); 30}; 31 32display = hexadecimal; 33print("rel error:", accurateinfnorm(1-poly(x)/f(x), [a;b], 30)); 34print("in [",a,b,"]"); 35print("coeffs:"); 36for i from 0 to deg do coeff(poly,i); 37