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