Bug in tanh?



On Sun, Mar 18, 2007 at 07:30:17PM -0500, Jay Belanger wrote:
> 
> "Richard Fateman" <fateman at cs.berkeley.edu> writes:
> 
> >  tanh(1.0d3)
> >   gives a stack overflow error in wxmaxima 0.7.1
> >
> > The answer should be 1.0d0.
> 
> Using the latest cvs Maxima compiled with sbcl, but not using
> wxmaxima, I get 
> 
> (%i1) tanh(1.0d3);
> (%o1) 				      1.0
> 
> > The computation of tanh(x) by using exp(x) is a bad idea.
> 
> What's the best way of computing it?

For floating point arguments, it's probably a good idea to simply
return 1.0 beyond whatever the cutoff for precision is. For bfloat
this would of course be variable depending on the fpprec flag...

The taylor series around 0 converges quickly for small values of
x. Other than that, perhaps a table of values and a taylor series
around each tabular value? I don't think they converge very well as x
increases though.  The radius of convergence is %pi/2 for the taylor
series around 0 according to wikipedia.

For large x perhaps a half angle formula recursively until the angle
is within range of your small table of values? Remembering to simply
cut off to 1.0 for x large enough... perhaps that's why the code
barfs??


-- 
Daniel Lakeland
dlakelan at street-artists.org
http://www.street-artists.org/~dlakelan