1// polynomial for approximating e^x 2// 3// Copyright (c) 2019, Arm Limited. 4// SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 5 6deg = 5; // poly degree 7N = 128; // table entries 8b = log(2)/(2*N); // interval 9b = b + b*0x1p-16; // increase interval for non-nearest rounding (TOINT_NARROW) 10a = -b; 11 12// find polynomial with minimal abs error 13 14// return p that minimizes |exp(x) - poly(x) - x^d*p(x)| 15approx = proc(poly,d) { 16 return remez(exp(x)-poly(x), deg-d, [a;b], x^d, 1e-10); 17}; 18 19// first 2 coeffs are fixed, iteratively find optimal double prec coeffs 20poly = 1 + x; 21for i from 2 to deg do { 22 p = roundcoefficients(approx(poly,i), [|D ...|]); 23 poly = poly + x^i*coeff(p,0); 24}; 25 26display = hexadecimal; 27print("rel error:", accurateinfnorm(1-poly(x)/exp(x), [a;b], 30)); 28print("abs error:", accurateinfnorm(exp(x)-poly(x), [a;b], 30)); 29print("in [",a,b,"]"); 30// double interval error for non-nearest rounding 31print("rel2 error:", accurateinfnorm(1-poly(x)/exp(x), [2*a;2*b], 30)); 32print("abs2 error:", accurateinfnorm(exp(x)-poly(x), [2*a;2*b], 30)); 33print("in [",2*a,2*b,"]"); 34print("coeffs:"); 35for i from 0 to deg do coeff(poly,i); 36