1 // polynomial for approximating sin(x)
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 deg = 15; // polynomial degree
11 // find even polynomial with minimal abs error compared to sin(x)/x
19 for i from 1 to 60 do { c = 2*i*(2*i + 1)*c; f = f + (-1)^i*x^(2*i)/c; };
21 // return p that minimizes |f(x) - poly(x) - x^d*p(x)|
22 approx = proc(poly,d) {
23 return remez(f(x)-poly(x), deg-d, [a;b], x^d, 1e-10);
26 // first coeff is fixed, iteratively find optimal double prec coeffs
28 for i from 1 to deg/2 do {
29 p = roundcoefficients(approx(poly,2*i), [|D ...|]);
30 poly = poly + x^(2*i)*coeff(p,0);
33 display = hexadecimal;
34 print("abs error:", accurateinfnorm(sin(x)-x*poly(x), [a;b], 30));
35 print("in [",a,b,"]");
37 for i from 0 to deg do coeff(poly,i);