I'm trying to assess the expected performance of calculating trigonometry functions as a function of the required precision. Obviously the wall clock time depends on the speed of the underlying arithmetic, so factoring that out by just counting number of operations:
Using state-of-the-art algorithms, how many arithmetic operations (add, subtract, multiply, divide) should it take to calculate sin(x)
, as a function of the number of bits (or decimal digits) of precision required in the output?
CodePudding user response:
... to assess the expected performance of calculating trigonometry functions as a function of the required precision.
Look as the first omitted term in the Taylor series sine for x = π/4
as the order of error.
Details: sin(x)
usually has these phases:
Handling special cases: NaN, infinities.
Argument reduction to the primary range to say [-π/4... π/4]. Real good reduction is hard as π is irrational and so involves code that reaches 50% of
sin()
time. Much time used to emulate the needed extended precision. (Research K.C. Ng's "ARGUMENT REDUCTION FOR HUGE ARGUMENTS: Good to the Last Bit")
Low quality reduction involves much less:/
, truncate,-
,*
.Calculation over a limited range. This is what many only consider. If done with a Taylor's series and needing 53 bits, then about 10-11 terms are needed: Taylor series sine. Yet quality code often uses a pair of crafted polynomials, each of about 4-5 terms, to form the quotient p(x)/q(x).
Of course dedicated hardware support in any of these steps greatly increases performance.
Note: code for
sin()
is often paired withcos()
code as extensive use of trig identities simplify the calculation.
I'd expect a software solution for sin()
to cost on the order of 25x a common *
. This is a rough estimate.
To achieve a very low error rate in the ULP, code typically uses a tad more. sine_crap()
could get by with only a few terms. So when assessing time performance, there is a trade-off with correctness. How good a sin()
do you want?
CodePudding user response:
We can calculate any trigonometric function using a simple right angled triangle or using the McLaurin\Taylor Series. So it really depends on which one you choose to implement. If you only pass an angle as an argument, and wish to calculate the sin of that particular angle, it would take about 4 to 6 steps to calculate the sin using an unit circle.