📄 trig.c
字号:
#ifndef lintstatic char *sccsid = " @(#)trig.c 4.1 (ULTRIX) 7/17/90";#endif lint/************************************************************************ * * * Copyright (c) 1986 by * * Digital Equipment Corporation, Maynard, MA * * All rights reserved. * * * * This software is furnished under a license and may be used and * * copied only in accordance with the terms of such license and * * with the inclusion of the above copyright notice. This * * software or any other copies thereof may not be provided or * * otherwise made available to any other person. No title to and * * ownership of the software is hereby transferred. * * * * This software is derived from software received from the * * University of California, Berkeley, and from Bell * * Laboratories. Use, duplication, or disclosure is subject to * * restrictions under license agreements with University of * * California and with AT&T. * * * * The information in this software is subject to change without * * notice and should not be construed as a commitment by Digital * * Equipment Corporation. * * * * Digital assumes no responsibility for the use or reliability * * of its software on equipment which is not supplied by Digital. * * * ************************************************************************//************************************************************************** Modification History** David Metsky 13-Jan-86** 001 Added from BSD 4.3 version as part of upgrade** Based on: trig.c 1.2 8/22/85**************************************************************************//* SIN(X), COS(X), TAN(X) * RETURN THE SINE, COSINE, AND TANGENT OF X RESPECTIVELY * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) * CODED IN C BY K.C. NG, 1/8/85; * REVISED BY W. Kahan and K.C. NG, 8/17/85. * * Required system supported functions: * copysign(x,y) * finite(x) * drem(x,p) * * Static kernel functions: * sin__S(z) ....sin__S(x*x) return (sin(x)-x)/x * cos__C(z) ....cos__C(x*x) return cos(x)-1-x*x/2 * * Method. * Let S and C denote the polynomial approximations to sin and cos * respectively on [-PI/4, +PI/4]. * * SIN and COS: * 1. Reduce the argument into [-PI , +PI] by the remainder function. * 2. For x in (-PI,+PI), there are three cases: * case 1: |x| < PI/4 * case 2: PI/4 <= |x| < 3PI/4 * case 3: 3PI/4 <= |x|. * SIN and COS of x are computed by: * * sin(x) cos(x) remark * ---------------------------------------------------------- * case 1 S(x) C(x) * case 2 sign(x)*C(y) S(y) y=PI/2-|x| * case 3 S(y) -C(y) y=sign(x)*(PI-|x|) * ---------------------------------------------------------- * * TAN: * 1. Reduce the argument into [-PI/2 , +PI/2] by the remainder function. * 2. For x in (-PI/2,+PI/2), there are two cases: * case 1: |x| < PI/4 * case 2: PI/4 <= |x| < PI/2 * TAN of x is computed by: * * tan (x) remark * ---------------------------------------------------------- * case 1 S(x)/C(x) * case 2 C(y)/S(y) y=sign(x)*(PI/2-|x|) * ---------------------------------------------------------- * * Notes: * 1. S(y) and C(y) were computed by: * S(y) = y+y*sin__S(y*y) * C(y) = 1-(y*y/2-cos__C(x*x)) ... if y*y/2 < thresh, * = 0.5-((y*y/2-0.5)-cos__C(x*x)) ... if y*y/2 >= thresh. * where * thresh = 0.5*(acos(3/4)**2) * * 2. For better accuracy, we use the following formula for S/C for tan * (k=0): let ss=sin__S(y*y), and cc=cos__C(y*y), then * * y+y*ss (y*y/2-cc)+ss * S(y)/C(y) = -------- = y + y * ---------------. * C C * * * Special cases: * Let trig be any of sin, cos, or tan. * trig(+-INF) is NaN, with signals; * trig(NaN) is that NaN; * trig(n*PI/2) is exact for any integer n, provided n*PI is * representable; otherwise, trig(x) is inexact. * * Accuracy: * trig(x) returns the exact trig(x*pi/PI) nearly rounded, where * * Decimal: * pi = 3.141592653589793 23846264338327 ..... * 53 bits PI = 3.141592653589793 115997963 ..... , * 56 bits PI = 3.141592653589793 227020265 ..... , * * Hexadecimal: * pi = 3.243F6A8885A308D313198A2E.... * 53 bits PI = 3.243F6A8885A30 = 2 * 1.921FB54442D18 error=.276ulps * 56 bits PI = 3.243F6A8885A308 = 4 * .C90FDAA22168C2 error=.206ulps * * In a test run with 1,024,000 random arguments on a VAX, the maximum * observed errors (compared with the exact trig(x*pi/PI)) were * tan(x) : 2.09 ulps (around 4.716340404662354) * sin(x) : .861 ulps * cos(x) : .857 ulps * * Constants: * The hexadecimal values are the intended ones for the following constants. * The decimal values may be used, provided that the compiler will convert * from decimal to binary accurately enough to produce the hexadecimal values * shown. */#ifdef VAX/*thresh = 2.6117239648121182150E-1 , Hex 2^ -1 * .85B8636B026EA0 *//*PIo4 = 7.8539816339744830676E-1 , Hex 2^ 0 * .C90FDAA22168C2 *//*PIo2 = 1.5707963267948966135E0 , Hex 2^ 1 * .C90FDAA22168C2 *//*PI3o4 = 2.3561944901923449203E0 , Hex 2^ 2 * .96CBE3F9990E92 *//*PI = 3.1415926535897932270E0 , Hex 2^ 2 * .C90FDAA22168C2 *//*PI2 = 6.2831853071795864540E0 ; Hex 2^ 3 * .C90FDAA22168C2 */static long threshx[] = { 0xb8633f85, 0x6ea06b02};#define thresh (*(double*)threshx)static long PIo4x[] = { 0x0fda4049, 0x68c2a221};#define PIo4 (*(double*)PIo4x)static long PIo2x[] = { 0x0fda40c9, 0x68c2a221};#define PIo2 (*(double*)PIo2x)static long PI3o4x[] = { 0xcbe34116, 0x0e92f999};#define PI3o4 (*(double*)PI3o4x)static long PIx[] = { 0x0fda4149, 0x68c2a221};#define PI (*(double*)PIx)static long PI2x[] = { 0x0fda41c9, 0x68c2a221};#define PI2 (*(double*)PI2x)#else /* IEEE double */static doublethresh = 2.6117239648121182150E-1 , /*Hex 2^ -2 * 1.0B70C6D604DD4 */PIo4 = 7.8539816339744827900E-1 , /*Hex 2^ -1 * 1.921FB54442D18 */PIo2 = 1.5707963267948965580E0 , /*Hex 2^ 0 * 1.921FB54442D18 */PI3o4 = 2.3561944901923448370E0 , /*Hex 2^ 1 * 1.2D97C7F3321D2 */PI = 3.1415926535897931160E0 , /*Hex 2^ 1 * 1.921FB54442D18 */PI2 = 6.2831853071795862320E0 ; /*Hex 2^ 2 * 1.921FB54442D18 */#endifstatic double zero=0, one=1, negone= -1, half=1.0/2.0, small=1E-10, /* 1+small**2==1; better values for small: small = 1.5E-9 for VAX D = 1.2E-8 for IEEE Double = 2.8E-10 for IEEE Extended */ big=1E20; /* big = 1/(small**2) */double tan(x) double x;{ double copysign(),drem(),cos__C(),sin__S(),a,z,ss,cc,c; int finite(),k; /* tan(NaN) and tan(INF) must be NaN */ if(!finite(x)) return(x-x); x=drem(x,PI); /* reduce x into [-PI/2, PI/2] */ a=copysign(x,one); /* ... = abs(x) */ if ( a >= PIo4 ) {k=1; x = copysign( PIo2 - a , x ); } else { k=0; if(a < small ) { big + a; return(x); }} z = x*x; cc = cos__C(z); ss = sin__S(z); z = z*half ; /* Next get c = cos(x) accurately */ c = (z >= thresh )? half-((z-half)-cc) : one-(z-cc); if (k==0) return ( x + (x*(z-(cc-ss)))/c ); /* sin/cos */ return( c/(x+x*ss) ); /* ... cos/sin */}double sin(x)double x;{ double copysign(),drem(),sin__S(),cos__C(),a,c,z; int finite(); /* sin(NaN) and sin(INF) must be NaN */ if(!finite(x)) return(x-x); x=drem(x,PI2); /* reduce x into [-PI, PI] */ a=copysign(x,one); if( a >= PIo4 ) { if( a >= PI3o4 ) /* .. in [3PI/4, PI ] */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -