[comp.lang.c] Testing math library

grosen@amadeus.ucsb.edu (Mark D. Grosen) (06/27/89)

I am writing a math library (equivalent to libm.a on most UNiX systems) for
a new processor.  (Functions like sin, cos, log, etc.)  I would appreciate
any suggestions on how to test these routines.  Are there any test suites
available, hopefully at a low cost?

Thanks.

Mark

Mark D. Grosen		ARPA: grosen@amadeus.ucsb.edu
Signal Processing Lab / Communications Research Lab
ECE Dept.
University of California
Santa Barbara, CA  93106

tps@chem.ucsd.edu (Tom Stockfisch) (06/28/89)

In article <2038@hub.ucsb.edu> grosen@amadeus.ucsb.edu (Mark D. Grosen) writes:
>
>I am writing a math library (equivalent to libm.a on most UNiX systems) for
>a new processor.  (Functions like sin, cos, log, etc.)  I would appreciate
>any suggestions on how to test these routines.

The first place I start testing mathematical functions is with bc(1).  If
you use the -l option you even get sin,cos,tan,exp,log already defined.
In case you don't know, bc is an arbitrary precision language with a C-like
syntax.  For the other functions I would write
brute force power series expansions evaluated to very
high terms to ensure there can be no round off errors in any digits of
interest.  It should also be simple
(and preferably different than your production algorithms)
so that gross errors in logic are less likely.

One thing you have to be careful about when you are determining the accuracy
of your routines:  bc(1) uses binary coded decimal, so that you have
to consider representational error in the arguments to functions.
E.g., sin(0.1) will have an error in the representation of its argument
in C (assuming binary), but not in bc.


-- 

|| Tom Stockfisch, UCSD Chemistry	tps@chem.ucsd.edu

dik@cwi.nl (Dik T. Winter) (06/29/89)

 In article <2038@hub.ucsb.edu> grosen@amadeus.ucsb.edu (Mark D. Grosen) writes:
 >
 > I am writing a math library (equivalent to libm.a on most UNiX systems) for
 > a new processor.  (Functions like sin, cos, log, etc.)  I would appreciate
 > any suggestions on how to test these routines.
 > 
You ought to have a look at the book "Software Manual for the Elementary
Functions" by Cody and Waite.  It describes how to implement these functions,
but also gives extensive test programs.  It contains a few errors, on the
other hand the descriptions are very clear and it describes a number of
pitfalls both in design as well as in testing.  It is however based on Fortran.
-- 
dik t. winter, cwi, amsterdam, nederland
INTERNET   : dik@cwi.nl
BITNET/EARN: dik@mcvax

bright@Data-IO.COM (Walter Bright) (06/29/89)

In article <2038@hub.ucsb.edu> grosen@amadeus.ucsb.edu (Mark D. Grosen) writes:
<I am writing a math library (equivalent to libm.a on most UNiX systems) for
<a new processor.  (Functions like sin, cos, log, etc.)  I would appreciate
<any suggestions on how to test these routines.  Are there any test suites
<available, hopefully at a low cost?

The best one is:
	"Software Manual For The Elementary Functions"
	by Cody and Waite
That is based on a denser book by Hart and Cheney, I forgot the title.
University libraries usually have these books. Highly recommended.

henry@utzoo.uucp (Henry Spencer) (06/29/89)

In article <2035@dataio.Data-IO.COM> bright@dataio.Data-IO.COM (Walter Bright) writes:
><I am writing a math library (equivalent to libm.a on most UNiX systems) for
><a new processor.  (Functions like sin, cos, log, etc.) ...
>
>The best one is:
>	"Software Manual For The Elementary Functions"

Note, however, that Cody&Waite is old.  If your machine uses IEEE floating
point (fairly likely these days), you should not be writing your own math
library -- you should be picking up the freely-redistributable 4.3BSD math
library, which had input from people like Kahan and is almost certainly
better than anything you can do yourself in any reasonable length of time.
Kahan commented, as I recall, that there is nothing wrong with Cody&Waite,
but their stuff is aimed at old, ill-behaved floating-point hardware, and
on IEEE hardware you can do better.

The 4.3 math library appeared in comp.sources.unix late last year.
-- 
NASA is to spaceflight as the  |     Henry Spencer at U of Toronto Zoology
US government is to freedom.   | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

rsalz@bbn.com (Rich Salz) (06/30/89)

>The 4.3 math library appeared in comp.sources.unix late last year.
And was then pulled because it contained some licensed ATT code.
If you have the comp.sources.unix posting on your machine and you're
not an ATT source licensee, please remove it or I will get sued and
all my wages garnisheed for ever and ever.
	/r$
-- 
Please send comp.sources.unix-related mail to rsalz@uunet.uu.net.
Use a domain-based address or give alternate paths, or you may lose out.

dik@cwi.nl (Dik T. Winter) (06/30/89)

In article <1870@prune.bbn.com> rsalz@bbn.com (Rich Salz) writes:
 > >The 4.3 math library appeared in comp.sources.unix late last year.
 > And was then pulled because it contained some licensed ATT code.
 > If you have the comp.sources.unix posting on your machine and you're
 > not an ATT source licensee, please remove it or I will get sued and
 > all my wages garnisheed for ever and ever.
 > 	/r$

In that case I hope Berkeley will replace all ATT code (how much? 10 lines?)
and distribute it again.  As Henry Spencer said, it is really the best you
can get.
-- 
dik t. winter, cwi, amsterdam, nederland
INTERNET   : dik@cwi.nl
BITNET/EARN: dik@mcvax

john@frog.UUCP (John Woods) (06/30/89)

In article <2035@dataio.Data-IO.COM>, bright@Data-IO.COM (Walter Bright) writes:
> The best one is:
> 	"Software Manual For The Elementary Functions"
> 	by Cody and Waite
> That is based on a denser book by Hart and Cheney, I forgot the title.

Computer Approximations, Hart et al, 1978 (reprint with corrections)
Robert E. Krieger Publishing Company, ISBN 0-88275-642-7.
Originally published by John Wiley & Sons.




-- 
John Woods, Charles River Data Systems, Framingham MA, (508) 626-1101
...!decvax!frog!john, john@frog.UUCP, ...!mit-eddie!jfw, jfw@eddie.mit.edu
    People...How you gonna FIGURE 'em?
    Don't bother, S.L.--Just stand back and enjoy the EVOLUTIONARY PROCESS...

housel@en.ecn.purdue.edu (Peter S. Housel) (07/07/89)

In article <1989Jun29.155325.28676@utzoo.uucp>, henry@utzoo (Henry Spencer) writes:
>In article <2035@dataio.Data-IO.COM> bright@dataio.Data-IO.COM (Walter Bright) writes:
>><I am writing a math library (equivalent to libm.a on most UNiX systems) for
>><a new processor.  (Functions like sin, cos, log, etc.) ...
>>The best one is:
>>	"Software Manual For The Elementary Functions"
>Note, however, that Cody&Waite is old.  If your machine uses IEEE floating
>point (fairly likely these days), you should not be writing your own math
>library -- you should be picking up the freely-redistributable 4.3BSD math
>library, which had input from people like Kahan and is almost certainly
>better than anything you can do yourself in any reasonable length of time.
>Kahan commented, as I recall, that there is nothing wrong with Cody&Waite,
>but their stuff is aimed at old, ill-behaved floating-point hardware, and
>on IEEE hardware you can do better.
>
>The 4.3 math library appeared in comp.sources.unix late last year.

I missed the original discussion, and this may be a stale topic by now,
but...

I have recently finished adding floating point to the Minix-PC C
compiler and C library. The bits are arranged the same way as most
IEEE implementations, but there is only one rounding mode (round-to-nearest)
and there are no NaN's or infinities.

Everything freely redistributable from 4.3Tahoe that will work has
gone into the pot. The new _doprnt() (renamed vfprintf()) worked
without much trouble. The VAX atof.s was translated almost verbatim
into C and 8086 assembly, and reworked into strtod().

However, after looking at the libm sources, I threw up my hands and
started reading up on numerical computation. No offense to the authors
intended, but this code is ugly! I can't make head nor tail of much of
it.

Sure, most of these programs are accurate to the last bit of the
significand. But the price is code that freely twiddles with the bits in
an opaque manner. Anyone who can figure out drem() or sqrt() in either
the C or assembly implementations is worthy of respect.

I have been stealing coefficient vectors from this code when writing
my own. The code is clean, behaves according to K&R2, and most of it
is numerically safe, but it isn't nearly as clever and accurate as the
4.3 libm.

Does anybody know if either Hart and Cheney, or Cody and Waite, are still
in print? I can't find either one in the local libraries. [The kind with
books in them, not object files.]

Also, was the proprietary code exorcised before the 4.3-Tahoe release was
made?  Can I assume that everything from the Tahoe tape with a "Freely
Redistributable" copyright, really is? Also, was it AT&T code, or CCI's?

(Expect whatever is done by then to be posted to comp.os.minix at the end
of this month.)

-Peter S. Housel-	housel@ecn.purdue.edu		...!pur-ee!housel

bright@Data-IO.COM (Walter Bright) (07/11/89)

In article <13185@ea.ecn.purdue.edu> housel@en.ecn.purdue.edu (Peter S. Housel) writes:
<Does anybody know if either Hart and Cheney, or Cody and Waite, are still
<in print? I can't find either one in the local libraries. [The kind with
<books in them, not object files.]

First, I'd try a university library. Second, the way I got the books was
to order them from the publisher through my local friendly bookstore.
(A lot of the chain bookstores won't order books for you. Boycott them!)