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