home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource3
/
141_01
/
cmath.c
< prev
next >
Wrap
Text File
|
1985-03-11
|
15KB
|
471 lines
/* cmath.c 1984 apr 30 pmk -
combine fpsqrt + clogs + ctrig
move consts to float.h & coef's to coef.h
speedup inner eval loops */
#include "float.h"
#include "coef.h"
char *fpsqrt (z, xin) char *z, *xin ; { /* z = sqrt (x) */
#define ITS 6 /* iteration count */
float x, z2, x2 ; unsigned n ;
fpasg (z, xin) ; fpasg (x, xin) ;
z[4] = (z[4] >> 1) | (0x80 & z[4]) ;
for (n = ITS ; n ; --n) {
/* Newton - Raphson, ITS iterations */
fpdiv (z2, x, z) ; /* z2 = x / z ; */
fpadd (z, z, z2) ; /* z += z2 ; */
fpmult (z, HALF, z) ; /* z *= half ; */
} /* for ITS */
return z ;
#undef ITS
} /* fpsqrt */
/* CTRIG.C *****
sine, cosine, tangent and arctangent
convert degrees to radians, convert radians to degrees
These functions are discussed in detail in CTRIG.DOC
L. C. Calhoun
257 South Broadway
Lebanon, Ohio 45036 513 932 4541/433 7510
*/
/* simple ones first converting degrees - radians */
char *degtorad(rad,deg) /*obvious arguments in 5 char fp */
char *rad, *deg;
{
char *fpmult();
fpmult(rad,deg,RADINDEG);
return (rad);
} /* degtorad */
char *radtodeg(deg,rad) /* 5 char fp arguments */
char *deg, *rad;
{
char *fpmult();
fpmult(deg,rad,DEGINRAD);
return (deg);
} /* radtodeg */
/* service function sinev which evaluates when range of angle
reduced to plus or minus pi/2 (90 deg) */
char *sinev(result,x) char *result, x[5]; {
char *fpmult(),xsq[5]; char *coef[5];
char *fpadd(),*fpasg();
int index;
/* use the exponent part of the floating point
to check for threat of underflow use small
angle approximation if appropriate */
if ( (x[4] > 128) && (x[4] < 226) )
{fpasg(result,x); return (result);
} /* solution to fpmult underflow problem */
/* series coef are 1., -.1666666, .008333026, -.0001980742,
.000002601887 determined from coefset program */
coef[0] = SINC0 ; coef[1] = SINC1 ;
coef[2] = SINC2 ; coef[3] = SINC3 ;
coef[4] = SINC4 ; fpmult(xsq,x,x) ;
/* to this point the coef have been initialized,
x squared computed. */
/* now to do the polynomial approximation */
fpasg (result, coef[4]) ;
for (index = 3 ; 0 <= index ; --index) {
fpadd (result, coef[index],
fpmult (result, result, xsq)) ;
} /* this loop approx 1/3 faster than original */
fpmult (result, result, x) ;
return (result);
} /* sinev */
/* here is sine(result,angle) with angle in radians */
char *sine(result,angle) char *result, *angle; {
char *fpmult(),*twopi,*halfpi;
char *mtwopi,*mhalfpi,*fpasg(),*fpchs();
char *pi,*sinev(),*fpadd(); char y[5],*fpsub();
int fpcomp(), compar; int signsine;
/* some initialization required here */
signsine = 1;
twopi = TWOPI ; halfpi = HALFPI ;
pi = PI ; mtwopi = MTWOPI ;
mhalfpi = MHALFPI ; fpasg(y,angle);
while (fpcomp(y,twopi) >= 0)
fpsub(y,y,twopi);
while (fpcomp(y,mtwopi)<= 0)
fpadd(y,y,twopi);
if(fpcomp(y,halfpi) > 0)
{ signsine *=-1; fpsub (y,y,pi); }
if(fpcomp(y,mhalfpi)< 0)
{ signsine *=-1; fpadd (y,y,pi); }
sinev(result,y);
if (signsine > 0) return (result);
/* minus so need to change sign */
else return ( fpchs(result,result) );
} /* sine */
/* cosine(result,angle) with angle in radians - uses sine */
char *cosine(result,angle) char *result, *angle; {
char *sine(),*fpsub(),y[5];
fpsub(y,HALFPI,angle);
sine(result,y);
return (result);
} /* cosine */
/* tangent(result,angle) with angle in radians, returns very
large number for divide by zero condition */
char *tangent(result,angle) char *result, angle[5]; {
char *sine(), *cosine(), *fpdiv(), zero[5];
char sresult[5], cresult[5], intres[5], big[5];
char *fpmult(), *fpmag();
sine(sresult,angle);
cosine(cresult,angle);
/* check magnitude of denominator :*/
/* check magnitude of denominator using exponent */
if ( (cresult[4] > 128) && (cresult[4] < 132) )
{initb(big,"30,207,228,127,128"); /*big number */
if ( sresult[3] > 127 ) /*use mantissa sign */
return ( fpchs(result,big) );
else return ( fpasg(result,big) );
}
/* check for small angle, use small angle approx to
avoid underflow */
if ( (angle[4] < 226) && (angle[4] > 128) )
return ( fpasg(result,angle) );
else
return ( fpdiv(result,sresult,cresult) );
} /* tangent */
/* atanev(result,x) evaluates arctangent for 0 <= x < infinity,
result in radians */
char *atanev(result,x) char result[5], x[5]; {
char *coef[8],y[5]; int index;
char *fpadd(),*fpmult(),*atof(),ysq[5];
char yterm[5],termreslt[5],*fpasg();
/* small angle approximation */
if ( (x[4] > 128) && (x[4] < 226) )
/* use fp exponent to check size, use small angle */
return ( fpasg(result,x) );
fpasg(result,PIOVER4);
/* check for argument near one */
fpsub(yterm,x,ONE);
if ( (yterm[4] > 128) && (yterm[4] < 243) )
return (result);
coef[0] = ATNC0 ; coef[1] = ATNC1 ;
coef[2] = ATNC2 ; coef[3] = ATNC3 ;
coef[4] = ATNC4 ; coef[5] = ATNC5 ;
coef[6] = ATNC6 ; coef[7] = ATNC7 ;
fpadd(termreslt,x,ONE);
fpdiv(y,yterm,termreslt);
fpmult(ysq,y,y);
/* do poly evaluation, fast loop */
for (index = 6, fpasg (result, coef[7]) ; 0 <= index ;
--index)
fpadd (result, coef[index],
fpmult (result, result, ysq)) ;
fpadd (result, PIOVER4, fpmult (result, result, y) ) ;
return (result);
} /* atanev */
/* arctan(result,angle) is floating point arctangent evaluation */
char *arctan(result,x) char result[5], x[5]; {
char *atanev(),*fpasg(),y[5];
char *fpchs(); int index;
/* check exponent for very large argument */
if ( (x[4] > 100) && (x[4] <= 128) )
fpasg(result,HALFPI);
else /* go through evaluation */
{ fpmag(y,x); atanev(result,y); }
if ( x[3] > 127 )
return ( fpchs(result,result) );
else return (result);
} /* arctan */
char *arctan2 (result, quadrant, opside, adjside)
char result[5], opside[5], adjside[5]; int *quadrant; {
char x[5], *fpmag(), *fpchs(); *arctan();
int opsign, adjsign;
char *zero ; zero = ZERO ;
opsign = fpcomp(opside,zero);
adjsign = fpcomp(adjside,zero);
if((adjsign == 0) && (opsign == 0))
{ *quadrant = 0; fpasg(result,zero);
return(result); }
if ( ( (128 < adjside[4]) && (adjside[4] < 226) )
|| (adjsign == 0) )
fpasg(result,HALFPI);
else
{ fpdiv(x,opside,adjside);
fpmag(x,x); arctan(result,x); }
if ( (adjsign == 0) && (opsign > 0) )
{ *quadrant = 1; return(result); }
if ( (adjsign == 0) && (opsign < 0) )
{ *quadrant = 4; fpchs(result,result);
return(result); }
if ( (adjsign > 0) && (opsign == 0) )
{ *quadrant = 1; return(result); }
if ( (adjsign < 0 ) && (opsign == 0) )
{ *quadrant = 2; fpchs(result,result);
return(result); }
if ( (adjsign > 0) && (opsign > 0) )
{ *quadrant = 1; return(result); }
if ( (adjsign > 0) && (opsign < 0) )
{ *quadrant = 4; fpchs(result,result);
return(result); }
if ( (adjsign < 0) && (opsign > 0) )
{ *quadrant = 2; fpchs(result,result);
return(result); }
if ( (adjsign < 0) && (opsign < 0) )
{ *quadrant = 3; return (result); }
} /* arctan2 */
/* CLOGS.C */
char *pi(result) char *result; {
char *fpasg();
return (fpasg(result,PI) );
} /* pi */
char *expe(result,xin)
/* computes the base of the natural log "e" raised to the "x'th"
power. Checks are made for out of range values and result is
defaulted to 0, 1, or a large number as appropriate. There are
no error flags. The arguments are floating point character
arrays char result[5], x[5]; in calling program. Return is
a pointer to result, and "e to the x" stored in result.
*/
char *result, *xin ; {
char *one, *coef[7], x[5] ;
char *fpmult(), *fpasg(), *fpdiv(), *fpchs();
int signx, index, bigexp, smallexp, zeroin;
int exprange();
bigexp = smallexp = zeroin = 0; one = ONE ;
/* preserve arg before transforms */
fpasg (x, xin) ;
coef[0] = EXPC0 ; coef[1] = EXPC1 ;
coef[2] = EXPC2 ; coef[3] = EXPC3 ;
coef[4] = EXP