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 = 4; // poly degree 7N = 128; // table entries 8b = log(2)/(2*N); // interval 9a = -b; 10 11// find polynomial with minimal abs error 12 13// return p that minimizes |exp(x) - poly(x) - x^d*p(x)| 14approx = proc(poly,d) { 15 return remez(exp(x)-poly(x), deg-d, [a;b], x^d, 1e-10); 16}; 17 18// first 2 coeffs are fixed, iteratively find optimal double prec coeffs 19poly = 1 + x; 20for i from 2 to deg do { 21 p = roundcoefficients(approx(poly,i), [|D ...|]); 22 poly = poly + x^i*coeff(p,0); 23}; 24 25display = hexadecimal; 26print("rel error:", accurateinfnorm(1-poly(x)/exp(x), [a;b], 30)); 27print("abs error:", accurateinfnorm(exp(x)-poly(x), [a;b], 30)); 28print("in [",a,b,"]"); 29print("coeffs:"); 30for i from 0 to deg do coeff(poly,i); 31