brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.1 KiB · 4107239 Raw
37 lines · plain
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-exception6 7deg = 12; // poly degree8// |log(1+x)| > 0x1p-4 outside the interval9a = -0x1p-4;10b =  0x1.09p-4;11 12// find log(1+x)/x polynomial with minimal relative error13// (minimal relative error polynomial for log(1+x) is the same * x)14deg = deg-1; // because of /x15 16// f = log(1+x)/x; using taylor series17f = 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 coeffs26poly = 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