1// polynomial for approximating sin(x) 2// 3// Copyright (c) 2019, Arm Limited. 4// SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 5 6deg = 15; // polynomial degree 7a = -pi/2; // interval 8b = pi/2; 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("abs error:", accurateinfnorm(sin(x)-x*poly(x), [a;b], 30)); 34print("in [",a,b,"]"); 35print("coeffs:"); 36for i from 0 to deg do coeff(poly,i); 37