trig functions
Dik T. Winter
dik at cwi.nl
Wed Nov 14 13:07:31 AEST 1990
(I did already respond to the original article, but follow-up now.)
In article <9011131959.AA11389@> lmo at lsr-vax.UUCP ("Lance M. Optican - LMO") writes:
> ----- Begin Included Message -----
...[lots of included message omitted]...
> Is it reasonable to expect the cosine of (exactly) 0 to be (exactly) 1?
> ----- End Included Message -----
> It is reasonable to expect the maht library to perform well!
Yes, but that does not imply that cos(0)=1.
> What standards
> of compatibility are enforced with other machine architectures?
What do you mean? Which machine architecture? Honeywell? (where cos(0)>1).
The problem is more complex than you think. When implementing cos and sin
you have two algorithmic choices (if you use additional precision you've
got a few more choices):
1. Promise that the functions are monotonic at pi/4, but do not promise that
cos(0)=1. This is what you get if you implement Cody-Waite's functions.
You might even end up with cos(0)>1. When you reduce the range of the
argument to (-pi/2,pi/2) and do an approximation there you end up with
this behaviour (not exactly true, but nearly).
2. Promise that cos(0)=1, but do not promise that the functions are monotonic
at pi/4 (i.e. in the neighbourhood of pi/4 it can happen that x>y but
sin(x)<sin(y)). This is the way I prefer it and implement them. You get
this behaviour if you reduce the range to (-pi/4,pi/4) and choose whether
you need a cosine or a sine approximation. This will also make the
numerical derivative at x=0 correct; but that is a moot point as it will
be very bad at x=pi/4!
In case 1 you can still make a choice:
3. Special case x=0. This may create non-monotonic behaviour in the
neighbourhood of x=0 (it might even be that some results in the
neighbourhood are larger than 1).
Still, even if cos(0)!=1 it may very well be within the accuracy constraints!
You should be much more apprehensive of implementations where sin(pi)=0. Throw
them away, they are wrong!
--
dik t. winter, cwi, amsterdam, nederland
dik at cwi.nl
More information about the Comp.sys.sgi
mailing list