brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.0 KiB · 5c59642 Raw
38 lines · plain
1// polynomial for approximating sin(x)2//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-exception6 7deg = 15;  // polynomial degree8a = -pi/2; // interval9b = pi/2;10 11// find even polynomial with minimal abs error compared to sin(x)/x12 13// account for /x14deg = deg-1;15 16// f = sin(x)/x;17f = 1;18c = 1;19for i from 1 to 60 do { c = 2*i*(2*i + 1)*c; f = f + (-1)^i*x^(2*i)/c; };20 21// return p that minimizes |f(x) - poly(x) - x^d*p(x)|22approx = proc(poly,d) {23  return remez(f(x)-poly(x), deg-d, [a;b], x^d, 1e-10);24};25 26// first coeff is fixed, iteratively find optimal double prec coeffs27poly = 1;28for i from 1 to deg/2 do {29  p = roundcoefficients(approx(poly,2*i), [|D ...|]);30  poly = poly + x^(2*i)*coeff(p,0);31};32 33display = hexadecimal;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