134127Sbostic /* 231931Szliu * Copyright (c) 1987 Regents of the University of California. 334127Sbostic * All rights reserved. 434127Sbostic * 5*42653Sbostic * %sccs.include.redist.c% 634127Sbostic * 734127Sbostic * All recipients should regard themselves as participants in an ongoing 834127Sbostic * research project and hence should feel obligated to report their 934127Sbostic * experiences (good or bad) with these elementary function codes, using 1034127Sbostic * the sendbug(8) program, to the authors. 1131931Szliu */ 1231931Szliu 1331931Szliu #ifndef lint 14*42653Sbostic static char sccsid[] = "@(#)sincos.c 5.4 (Berkeley) 06/01/90"; 1534127Sbostic #endif /* not lint */ 1631931Szliu 1731931Szliu #include "trig.h" 1831931Szliu double 1931931Szliu sin(x) 2031931Szliu double x; 2131931Szliu { 2231931Szliu double a,c,z; 2331931Szliu 2431931Szliu if(!finite(x)) /* sin(NaN) and sin(INF) must be NaN */ 2531931Szliu return x-x; 2631931Szliu x=drem(x,PI2); /* reduce x into [-PI,PI] */ 2731931Szliu a=copysign(x,one); 2831931Szliu if (a >= PIo4) { 2931931Szliu if(a >= PI3o4) /* ... in [3PI/4,PI] */ 3031931Szliu x = copysign((a = PI-a),x); 3131931Szliu else { /* ... in [PI/4,3PI/4] */ 3231931Szliu a = PIo2-a; /* rtn. sign(x)*C(PI/2-|x|) */ 3331931Szliu z = a*a; 3431931Szliu c = cos__C(z); 3531931Szliu z *= half; 3631931Szliu a = (z >= thresh ? half-((z-half)-c) : one-(z-c)); 3731931Szliu return copysign(a,x); 3831931Szliu } 3931931Szliu } 4031931Szliu 4131931Szliu if (a < small) { /* rtn. S(x) */ 4231931Szliu big+a; 4331931Szliu return x; 4431931Szliu } 4531931Szliu return x+x*sin__S(x*x); 4631931Szliu } 4731931Szliu 4831931Szliu double 4931931Szliu cos(x) 5031931Szliu double x; 5131931Szliu { 5231931Szliu double a,c,z,s = 1.0; 5331931Szliu 5431931Szliu if(!finite(x)) /* cos(NaN) and cos(INF) must be NaN */ 5531931Szliu return x-x; 5631931Szliu x=drem(x,PI2); /* reduce x into [-PI,PI] */ 5731931Szliu a=copysign(x,one); 5831931Szliu if (a >= PIo4) { 5931931Szliu if (a >= PI3o4) { /* ... in [3PI/4,PI] */ 6031931Szliu a = PI-a; 6131931Szliu s = negone; 6231931Szliu } 6331931Szliu else { /* ... in [PI/4,3PI/4] */ 6431931Szliu a = PIo2-a; 6531931Szliu return a+a*sin__S(a*a); /* rtn. S(PI/2-|x|) */ 6631931Szliu } 6731931Szliu } 6831931Szliu if (a < small) { 6931931Szliu big+a; 7031931Szliu return s; /* rtn. s*C(a) */ 7131931Szliu } 7231931Szliu z = a*a; 7331931Szliu c = cos__C(z); 7431931Szliu z *= half; 7531931Szliu a = (z >= thresh ? half-((z-half)-c) : one-(z-c)); 7631931Szliu return copysign(a,s); 7731931Szliu } 78