home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
212_01
/
floatr.c
< prev
next >
Wrap
Text File
|
1979-12-31
|
30KB
|
1,326 lines
/* FLOATR.C VERS:- 01.00 DATE:- 09/26/86 TIME:- 09:36:35 PM */
/*
%CC1 $1.C
*/
/*
Description:
Library of floating point functions (FLOAT.C, by Bob Mathias)
with additions (FLOAT+44.C, by L.C. Calhoun)
with added trig functions (CTRIG.C, by L.C. Calhoun)
with added log functions (CLOGS.C, by L.C. Calhoun)
with additional function(s) (by J.A. Rupley)
FLOAT.C from BDS C distribution package, version 1.5
FLOAT+44.C, CTRIG.C, AND CLOGS.C are from cug disk "functions III".
Note: the v1.5 FLOAT.C has an updated version of _spr;
replaced v1.44 std functions by v1.5 std functions
By J.A. Rupley, Tucson, Arizona
Coded for BDS C compiler, version 1.50a
*/
/* page eject */
/* summary of functions
(from FLOAT, by Mathias)
int fpcomp(op1,op2) return = (1,0,-1) || (op1 >,=,< op2)
char *fpadd(result,op1,op2) result = op1 + op2
char *fpsub(result,op1,op2) result = op1 - op2
char *fpmult(result,op1,op2) result = op1*op2
char *fpdiv(result,op1,op2) result = op1/op2
char *itof(result,int#) result = float(int#)
char *atof(result,string#) result = float(string#)
char *ftoa(result,fpno) result = string(fpno)
char *itoa(result,int#) result = string(int#)
void printf(format) standard C print function
void _spr(line,&format) float formatting function
(from FLOAT+44, by Calhoun)
char *fpmag(result,fpno) result = abs(fpno)
char *fpchs(result,fpno) result = - fpno
char *fpasg(result,fpno) result = fpno
int ftoit(fpno) return = truncate then to integer
int ftoir(fpno) return = round then to integer
(from CLOGS, by Calhoun)
int exprange(fpno) service function
char *pi(result) result = float(pi)
char *expe(result,fpno) result = e^(fpno)
char *exp10(result,fpno) result = 10^(fpno)
char *log10(result,&int,fpno) result = log10(abs(fpno)) //
*int = sgn(fpno)
(from CTRIG, by Calhoun)
char *degtorad(result,fpno) result = rad(fpno-degrees)
char *radtodeg(result,fpno) result = deg(fpno-radians)
char *sinev(result,fpno) service function
char *sine(result,fpno) result = sin(fpno-radians)
char *cosine(result,fpno) result = cos(fpno-radians)
char *tangent(result,fpno) result = tangent(fpno-radians)
char *atanev(result,fpno) service function
char *arctan(result,fpno) result = arctan-radians(fpno)
char *arctan2(result,quadrant,opside,adjside)
result = arctan-radians(opside/adjside) // quadrant = 1 to 4
(additions, by Rupley)
char *sqrt(result,&int,fpno) result = sqrt(abs(fpno)) //
*int = sgn(fpno)
*/
/* page eject */
/* FLOAT.C ***************/
/*
Floating point package support routines
Note the "fp" library function, available in DEFF2.CRL,
is used extensively by all the floating point number
crunching functions.
-------------------------------------------------------------
Usage: After compiling your program, link with this library
by typing:
A>clink <your program files> -f float <cr>
-------------------------------------------------------------
NEW FEATURE: a special "printf" function has been included
in this source file for use with floating point
operands, in addition to the normal types. The
printf presented here will take precedence over
the DEFF.CRL version when "float" is specified
on the CLINK command line at linkage time.
Note that the "fp" function, needed by most of
the functions in this file, resides in DEFF2.CRL
and will be automatically collected by CLINK.
All functions here written by Bob Mathias, except printf and
_spr (written by Leor Zolman.)
*/
#include <bdscio.h>
#define NORM_CODE 0
#define ADD_CODE 1
#define SUB_CODE 2
#define MULT_CODE 3
#define DIV_CODE 4
#define FTOA_CODE 5
fpcomp(op1, op2)
char *op1, *op2;
{
char work[5];
fpsub(work, op1, op2);
if (work[3] > 127)
return (-1);
if (work[0] + work[1] + work[2] + work[3])
return (1);
return (0);
}
fpnorm(op1) char *op1;
{
fp(NORM_CODE, op1, op1);
return (op1);
}
fpadd(result, op1, op2)
char *result, *op1, *op2;
{
fp(ADD_CODE, result, op1, op2);
return (result);
}
fpsub(result, op2, op1)
char *result, *op1, *op2;
{
fp(SUB_CODE, result, op1, op2);
return (result);
}
fpmult(result, op1, op2)
char *result, *op1, *op2;
{
fp(MULT_CODE, result, op1, op2);
return (result);
}
fpdiv(result, op1, op2)
char *result, *op1, *op2;
{
fp(DIV_CODE, result, op1, op2);
return (result);
}
atof(fpno, s)
char fpno[5], *s;
{
char *fpnorm(), work[5], ZERO[5], FP_10[5];
int sign_boolean, power;
initb(FP_10, "0,0,0,80,4");
setmem(fpno, 5, 0);
sign_boolean = power = 0;
while (*s == ' ' || *s == '\t')
++s;
if (*s == '-')
{
sign_boolean = 1;
++s;
}
for (; isdigit(*s); ++s)
{
fpmult(fpno, fpno, FP_10);
work[0] = *s - '0';
work[1] = work[2] = work[3] = 0;
work[4] = 31;
fpadd(fpno, fpno, fpnorm(work));
}
if (*s == '.')
{
++s;
for (; isdigit(*s); --power, ++s)
{
fpmult(fpno, fpno, FP_10);
work[0] = *s - '0';
work[1] = work[2] = work[3] = 0;
work[4] = 31;
fpadd(fpno, fpno, fpnorm(work));
}
}
if (toupper(*s) == 'E')
{
++s;
power += atoi(s);
}
if (power > 0)
for (; power != 0; --power)
fpmult(fpno, fpno, FP_10);
else
if (power < 0)
for (; power != 0; ++power)
fpdiv(fpno, fpno, FP_10);
if (sign_boolean)
{
setmem(ZERO, 5, 0);
fpsub(fpno, ZERO, fpno);
}
return (fpno);
}
ftoa(result, op1)
char *result, *op1;
{
fp(FTOA_CODE, result, op1);
return (result);
}
itof(op1, n)
char *op1;
int n;
{
int *p;
p = op1;
p[0] = 0;
p[1] = n;
op1[4] = 15;
fp(NORM_CODE, op1, op1);
return op1;
}
itoa(str, n)
char *str;
{
char *sptr;
sptr = str;
if (n < 0)
{
*sptr++ = '-';
n = -n;
}
_uspr(&sptr, n, 10);
*sptr = '\0';
return str;
}
/*
This is the special formatting function, which supports the
"e" and "f" conversions as well as the normal "d", "s", etc.
When using "e" or "f" format, the corresponding argument in
the argument list should be a pointer to one of the five-byte
strings used as floating point numbers by the floating point
functions. Note that you don't need to ever use the "ftoa"
function when using this special printf/sprintf combination;
to achieve the same result as ftoa, a simple "%e" format
conversion will do the trick. "%f" is used to eliminate the
scientific notation and set the precision. The only [known]
difference between the "e" and "f" conversions as used here
and the ones described in the Kernighan & Ritchie book is that
ROUNDING does not take place in this version...e.g., printing
a floating point number which happens to equal exactly 3.999
using a "%5.2f" format conversion will produce " 3.99" instead
of " 4.00".
*/
printf(format)
char *format;
{
int putchar();
_spr(&format, &putchar); /* use "_spr" to form the output */
}
_spr(fmt, putcf, arg1)
int (*putcf)();
char **fmt;
{
char _uspr(), c, base, *sptr, *format;
char wbuf[MAXLINE], *wptr, pf, ljflag, zfflag;
int width, precision, exp, *args;
format = *fmt++; /* fmt first points to the format string */
args = fmt; /* now fmt points to the first arg value */
while (c = *format++)
if (c == '%')
{
wptr = wbuf;
precision = 6;
ljflag = pf = zfflag = 0;
if (*format == '-')
{
format++;
ljflag++;
}
if (*format == '0')
zfflag++; /* test for zero fill */
width = isdigit(*format) ? _gv2(&format) : 0;
if ((c = *format++) == '.')
{
precision = _gv2(&format);
pf++;
c = *format++;
}
switch (toupper(c))
{
case 'E' :
if (precision > 7)
precision = 7;
ftoa(wbuf, *args++);
strcpy(wbuf + precision + 3, wbuf + 10);
width -= strlen(wbuf);
goto pad2;
case 'F' :
ftoa(&wbuf[60], *args++);
sptr = &wbuf[60];
while (*sptr++ != 'E')
;
exp = atoi(sptr);
sptr = &wbuf[60];
if (*sptr == ' ')
sptr++;
if (*