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