brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.2 KiB · 1877b07 Raw
37 lines · plain
1// polynomial for approximating e^x2//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 = 5; // poly degree8N = 128; // table entries9b = log(2)/(2*N);  // interval10b = b + b*0x1p-16; // increase interval for non-nearest rounding (TOINT_NARROW)11a = -b;12 13// find polynomial with minimal abs error14 15// return p that minimizes |exp(x) - poly(x) - x^d*p(x)|16approx = proc(poly,d) {17  return remez(exp(x)-poly(x), deg-d, [a;b], x^d, 1e-10);18};19 20// first 2 coeffs are fixed, iteratively find optimal double prec coeffs21poly = 1 + x;22for i from 2 to deg do {23  p = roundcoefficients(approx(poly,i), [|D ...|]);24  poly = poly + x^i*coeff(p,0);25};26 27display = hexadecimal;28print("rel error:", accurateinfnorm(1-poly(x)/exp(x), [a;b], 30));29print("abs error:", accurateinfnorm(exp(x)-poly(x), [a;b], 30));30print("in [",a,b,"]");31// double interval error for non-nearest rounding32print("rel2 error:", accurateinfnorm(1-poly(x)/exp(x), [2*a;2*b], 30));33print("abs2 error:", accurateinfnorm(exp(x)-poly(x), [2*a;2*b], 30));34print("in [",2*a,2*b,"]");35print("coeffs:");36for i from 0 to deg do coeff(poly,i);37