diff --git a/sin.c b/sin.c index d1e0b25..1d426da 100644 --- a/sin.c +++ b/sin.c @@ -59,6 +59,12 @@ static double atanlist[100] = { 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 i = 0; @@ -123,11 +129,11 @@ double sin11(double theta, int *it, double *dsin) xy[0] = x0; xy[1] = y0; dxy[1] = 1; - for (i = 0; i < 56; i++) + for (i = 0; i < 55; i++) { - d = alpha > 0 ? 1 : (alpha < 0 ? -1 : 0); - dxy[0] = -d * xy[1] * t; - dxy[1] = d * xy[0] * t; + // d = sign(alpha); + dxy[0] = -xy[1] * copysign(t,alpha); + dxy[1] = xy[0] * copysign(t,alpha); xy[0] += dxy[0]; xy[1] += dxy[1]; alpha -= d * atanlist[i]; @@ -176,16 +182,16 @@ int main() for (int i = 0; i < 5; i++) { start = clock(); - for (int j = 0; j < 10000; j++) + for (int j = 0; j < 10000000; j++) sin = sin11(thetalist[i], &it, &dsin); 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(); - for (int j = 0; j < 10000; j++) + for (int j = 0; j < 10000000; j++) sin = sin2(thetalist[i], &it, &dsin); 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;