简体   繁体   中英

Compute cosines and sines of a sequence of angles

I should create a program which computes the cosines and sines of a sequence of angles k*α, where k is a growing natural number (ie, 0, 1, 2,...) and α is a constant angle which lies between 0 and π. I would like to make this program as fast as possibile.

Hence, I want to compute first the cosine of each angle, and then the related sine with sqrt(1-cos(k*α)^2) . The problem is the sign of the sine, which should be determined by the position of the angle k*α on the real line.

I would like to know how I could implement this dynamic comparison as fast as possibile, or if the fastest way to proceed is to compute directly the sine, too.

After some time, I thought again about this problem and I found a really simple solution:

n = floor(k*alpha/pi);

if (n % 2 == 0)

    sin_alpha = +sqrt(1-pow(cos(k*alpha,2)));

else

    sin_alpha = -sqrt(1-pow(cos(k*alpha,2)));

Problem solved. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM