This is fptest.c in view mode; [Download] [Up]
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <sys/resource.h>
struct sopinfo {
char *name;
double (*function)(double x);
double lo;
double hi;
};
struct sopinfo soptab[] = {
{ "sqrt", sqrt, 0.0, 1.0e12 },
{ "sin", sin, -10.0, 10.0 },
{ "cos", cos, -10.0, 10.0 },
{ "tan", tan, -10.0, 10.0 },
{ "asin", asin, -1.0, 1.0 },
{ "acos", acos, -1.0, 1.0 },
{ "atan", atan, -10.0, 10.0 },
{ "exp", exp, -10.0, 10.0 },
{ "expm1", expm1, -10.0, 10.0 },
{ "log", log, 0.000001, 10.0 },
{ "log1p", log1p, -0.999999, 10.0 },
{ "sinh", sinh, -5.0, 5.0 },
{ "cosh", cosh, -5.0, 5.0 },
{ "tanh", tanh, -5.0, 5.0 },
{ "asinh", asinh, -5.0, 5.0 },
{ "acosh", acosh, 1.0, 10.0 },
{ "atanh", atanh, -1.0, 1.0 },
{ NULL, NULL, 0.0, 0.0 }
};
struct dopinfo {
char *name;
double (*function)(double x, double y);
double xlo;
double xhi;
double ylo;
double yhi;
};
struct dopinfo doptab[] = {
{ "pow", pow, -1.0, 9.0, -10.0, 10.0 },
{ "atan2", atan2, -10.0, 10.0, -10.0, 10.0 },
{ NULL, NULL, 0.0, 0.0, 0.0, 0.0 },
};
double x, y, z;
static void
sopfunc (struct sopinfo *sop, int verbose, long passcount)
{
long pass;
double inc = (sop->hi - sop->lo) / passcount;
for (pass = 0, x = sop->lo ; pass < passcount ; pass++, x+= inc) {
y = (sop->function)(x);
if (verbose)
printf ("%24.16g %24.16g\n", x, y);
}
}
static void
gfunc (struct dopinfo *dop, int verbose, long passcount)
{
long pass, xp;
long xplimit = sqrt ((double)passcount);
double xinc = (dop->xhi - dop->xlo) / xplimit;
double yinc = (dop->yhi - dop->ylo) / xplimit;
xp = 0;
x = dop->xlo;
y = dop->ylo;
for (pass = 0, x = dop->xlo ; pass < passcount ; pass++) {
z = (dop->function)(x, y);
if (verbose)
printf ("%24.16g %24.16g %24.16g\n", x, y, z);
if (++xp < xplimit) {
y += yinc;
}
else {
xp = 0;
x += xinc;
y = dop->ylo;
}
}
}
double
microseconds (struct timeval *tp)
{
return (tp->tv_sec * 1.0e6 + tp->tv_usec);
}
void
showtiming (char *name, long passcount, struct rusage *start, struct rusage *finish)
{
double u, s, t;
u = microseconds (&finish->ru_utime) - microseconds (&start->ru_utime);
s = microseconds (&finish->ru_stime) - microseconds (&start->ru_stime);
t = u + s;
printf ("%s: microseconds/call User:%g System:%g Total:%g\n", name,
u / passcount,
s / passcount,
t / passcount);
}
int
main (int argc, char **argv)
{
int i;
int verbose = 0;
long passcount = 10;
struct rusage start, finish;
struct sopinfo *sop;
struct dopinfo *dop;
extern int optind;
extern char *optarg;
while ((i = getopt (argc, argv, "vn:")) != EOF) {
switch (i) {
case 'v':
verbose = 1;
break;
case 'n':
passcount = atol (optarg);
break;
}
}
for (sop = soptab ; sop->name != NULL ; sop++) {
if (verbose)
printf ("*********************** %s ***********************\n", sop->name);
getrusage (RUSAGE_SELF, &start);
sopfunc (sop, verbose, passcount);
getrusage (RUSAGE_SELF, &finish);
if (!verbose)
showtiming (sop->name, passcount, &start, &finish);
}
for (dop = doptab ; dop->name != NULL ; dop++) {
if (verbose)
printf ("*********************** %s ***********************\n", dop->name);
getrusage (RUSAGE_SELF, &start);
gfunc (dop, verbose, passcount);
getrusage (RUSAGE_SELF, &finish);
if (!verbose)
showtiming (dop->name, passcount, &start, &finish);
}
return (0);
}
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.