base/math/s_cos.c

Go to the documentation of this file.
00001 /* @(#)s_cos.c 5.1 93/09/24 */
00002 /*
00003  * ====================================================
00004  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00005  *
00006  * Developed at SunPro, a Sun Microsystems, Inc. business.
00007  * Permission to use, copy, modify, and distribute this
00008  * software is freely granted, provided that this notice 
00009  * is preserved.
00010  * ====================================================
00011  */
00012 
00013 #if defined(LIBM_SCCS) && !defined(lint)
00014 static char rcsid[] = "$NetBSD: s_cos.c,v 1.7 1995/05/10 20:47:02 jtc Exp $";
00015 #endif
00016 
00017 /* cos(x)
00018  * Return cosine function of x.
00019  *
00020  * kernel function:
00021  *  __kernel_sin        ... sine function on [-pi/4,pi/4]
00022  *  __kernel_cos        ... cosine function on [-pi/4,pi/4]
00023  *  __ieee754_rem_pio2  ... argument reduction routine
00024  *
00025  * Method.
00026  *      Let S,C and T denote the sin, cos and tan respectively on 
00027  *  [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 
00028  *  in [-pi/4 , +pi/4], and let n = k mod 4.
00029  *  We have
00030  *
00031  *          n        sin(x)      cos(x)        tan(x)
00032  *     ----------------------------------------------------------
00033  *      0          S       C         T
00034  *      1          C      -S        -1/T
00035  *      2         -S      -C         T
00036  *      3         -C       S        -1/T
00037  *     ----------------------------------------------------------
00038  *
00039  * Special cases:
00040  *      Let trig be any of sin, cos, or tan.
00041  *      trig(+-INF)  is NaN, with signals;
00042  *      trig(NaN)    is that NaN;
00043  *
00044  * Accuracy:
00045  *  TRIG(x) returns trig(x) nearly rounded 
00046  */
00047 
00048 #include "math.h"
00049 #include "mathP.h"
00050 
00051 #ifdef __STDC__
00052     double cos(double x)
00053 #else
00054     double cos(x)
00055     double x;
00056 #endif
00057 {
00058     double y[2],z=0.0;
00059     int32_t n, ix;
00060 
00061     /* High word of x. */
00062     GET_HIGH_WORD(ix,x);
00063 
00064     /* |x| ~< pi/4 */
00065     ix &= 0x7fffffff;
00066     if(ix <= 0x3fe921fb) return __kernel_cos(x,z);
00067 
00068     /* cos(Inf or NaN) is NaN */
00069     else if (ix>=0x7ff00000) return x-x;
00070 
00071     /* argument reduction needed */
00072     else {
00073         n = __ieee754_rem_pio2(x,y);
00074         switch(n&3) {
00075         case 0: return  __kernel_cos(y[0],y[1]);
00076         case 1: return -__kernel_sin(y[0],y[1],1);
00077         case 2: return -__kernel_cos(y[0],y[1]);
00078         default:
00079                 return  __kernel_sin(y[0],y[1],1);
00080         }
00081     }
00082 }

Generated on Tue Feb 2 17:46:05 2010 for RTAI API by  doxygen 1.4.7