Szabolcs Nagy | d1db92e | 2019-07-18 10:14:45 +0100 | [diff] [blame] | 1 | // polynomial for approximating sin(x) |
| 2 | // |
| 3 | // Copyright (c) 2019, Arm Limited. |
| 4 | // SPDX-License-Identifier: MIT |
| 5 | |
| 6 | deg = 7; // polynomial degree |
| 7 | a = -pi/4; // interval |
| 8 | b = pi/4; |
| 9 | |
| 10 | // find even polynomial with minimal abs error compared to sin(x)/x |
| 11 | |
| 12 | // account for /x |
| 13 | deg = deg-1; |
| 14 | |
| 15 | // f = sin(x)/x; |
| 16 | f = 1; |
| 17 | c = 1; |
| 18 | for 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)| |
| 21 | approx = 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 |
| 26 | poly = 1; |
| 27 | for 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 | |
| 32 | display = hexadecimal; |
| 33 | print("rel error:", accurateinfnorm(1-poly(x)/f(x), [a;b], 30)); |
| 34 | print("abs error:", accurateinfnorm(sin(x)-x*poly(x), [a;b], 30)); |
| 35 | print("in [",a,b,"]"); |
| 36 | print("coeffs:"); |
| 37 | for i from 0 to deg do coeff(poly,i); |