update: use copysign() instead of d=sign(alpha)

This commit is contained in:
Yingjie Wang 2024-06-30 20:13:30 -04:00
parent df5933ed5c
commit 76dc412053

22
sin.c
View File

@ -59,6 +59,12 @@ static double atanlist[100] = {
3.1554436208840472216469142611311e-30, 1.5777218104420236108234571305656e-30 3.1554436208840472216469142611311e-30, 1.5777218104420236108234571305656e-30
}; };
int sign(double alpha)
{
return alpha > 0 ? 1 : (alpha < 0 ? -1 : 0);
// return (int)copysign(1.0, alpha);
}
int trig(double theta, double *xy, double *dxy) int trig(double theta, double *xy, double *dxy)
{ {
int i = 0; int i = 0;
@ -123,11 +129,11 @@ double sin11(double theta, int *it, double *dsin)
xy[0] = x0; xy[0] = x0;
xy[1] = y0; xy[1] = y0;
dxy[1] = 1; dxy[1] = 1;
for (i = 0; i < 56; i++) for (i = 0; i < 55; i++)
{ {
d = alpha > 0 ? 1 : (alpha < 0 ? -1 : 0); // d = sign(alpha);
dxy[0] = -d * xy[1] * t; dxy[0] = -xy[1] * copysign(t,alpha);
dxy[1] = d * xy[0] * t; dxy[1] = xy[0] * copysign(t,alpha);
xy[0] += dxy[0]; xy[0] += dxy[0];
xy[1] += dxy[1]; xy[1] += dxy[1];
alpha -= d * atanlist[i]; alpha -= d * atanlist[i];
@ -176,16 +182,16 @@ int main()
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
start = clock(); start = clock();
for (int j = 0; j < 10000; j++) for (int j = 0; j < 10000000; j++)
sin = sin11(thetalist[i], &it, &dsin); sin = sin11(thetalist[i], &it, &dsin);
end = clock(); end = clock();
printf("sin11(%.16g) = %.16g, it = %d, dsin=%.16g, took %.16g s.\n", thetalist[i], sin, it, dsin, (double)(end - start) / CLOCKS_PER_SEC); printf("sin11(%.16g) = %.16g, it = %d, dsin=%.16g, took %f s.\n", thetalist[i], sin, it, dsin, (double)(end - start) / CLOCKS_PER_SEC);
start = clock(); start = clock();
for (int j = 0; j < 10000; j++) for (int j = 0; j < 10000000; j++)
sin = sin2(thetalist[i], &it, &dsin); sin = sin2(thetalist[i], &it, &dsin);
end = clock(); end = clock();
printf("sin2(%.16g) = %.16g, it = %d, dsin=%.16g, took %.16g s.\n", thetalist[i], sin, it, dsin, (double)(end - start) / CLOCKS_PER_SEC); printf("sin2(%.16g) = %.16g, it = %d, dsin=%.16g, took %f s.\n", thetalist[i], sin, it, dsin, (double)(end - start) / CLOCKS_PER_SEC);
} }
return 0; return 0;