xref: /freebsd/contrib/arm-optimized-routines/math/tools/cos.sollya (revision 7ab1a32cd43cbae61ad4dd435d6a482bbf61cb52)
1// polynomial for approximating cos(x)
2//
3// Copyright (c) 2019, Arm Limited.
4// SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
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