1// polynomial for approximating cos(x)
2//
3// Copyright (c) 2019, Arm Limited.
4// SPDX-License-Identifier: MIT
5
6deg = 8;   // polynomial degree
7a = -pi/4; // interval
8b = pi/4;
9
10// find even polynomial with minimal abs error compared to cos(x)
11
12f = cos(x);
13
14// return p that minimizes |f(x) - poly(x) - x^d*p(x)|
15approx = proc(poly,d) {
16  return remez(f(x)-poly(x), deg-d, [a;b], x^d, 1e-10);
17};
18
19// first coeff is fixed, iteratively find optimal double prec coeffs
20poly = 1;
21for i from 1 to deg/2 do {
22  p = roundcoefficients(approx(poly,2*i), [|D ...|]);
23  poly = poly + x^(2*i)*coeff(p,0);
24};
25
26display = hexadecimal;
27print("rel error:", accurateinfnorm(1-poly(x)/f(x), [a;b], 30));
28print("abs error:", accurateinfnorm(f(x)-poly(x), [a;b], 30));
29print("in [",a,b,"]");
30print("coeffs:");
31for i from 0 to deg do coeff(poly,i);
32