Haskell 98 - Standard Prelude - Floating Class

Dylan Thurston dpt@math.harvard.edu
Tue, 16 Oct 2001 01:42:22 +0900


On Mon, Oct 15, 2001 at 06:27:52PM +0200, George Russell wrote:
> Simon PJ wrote:
> > Fpr the Revised Haskell 98 report, Russell O'Connor suggests:
> >               =20
> > | Also, I understand you are reluctant to make library changes,=20
> > | but sinh and cosh can easily be defined in terms of exp
> > |=20
> > | sinh x =3D (exp(x) - exp(-x))/2
> > | cosh x =3D (exp(x) + exp(-x))/2
> > |=20
> > | (source: Calculus Third Edition by Michael Spivak, page 349,=20
> > | or any decent calculus book)
> > |=20
> > | I suggest removing sinh and cosh from the minimal complete=20
> > | definition, and add the above defaults.
> > 
> > This looks pretty reasonable to me.  We should have default methods
> > for anything we can.
> > 
> > Comments?
> No.  As has been pointed out, this is a bad idea numerically because
> it will give the wrong answer for sinh x for very small values of
> x.  As a matter of fact, you will also get the wrong answer for very large
> values of x, where exp(x) can overflow even though sinh x and cosh x don't,
> meaning you get an incorrect answer of positive infinity.

Err, what?  Surely sinh x is at least 1/2 of exp x, leaving only a
very narrow range for this to happen.  Behaviour of sinh x near 0 is
more important, unless I'm missing something?

> I suggest saying as little about the transcendental functions as
> possible, rather than forcing incorrect rules on the implementor.

The suggestions are for default methods, which force nothing on
anybody.  They would be suggestions, in case anyone writes their own
instances of these classes; the question should be whether they are
useful suggestions.  For instance, if you have a class of computable
reals (increasingly good approximations), the default definitions of
sinh and cosh are excellent.

I don't think it's worth worrying about much.

(They don't work for floating point numbers because of the special
behaviour near 0.)

Best,
	Dylan Thurston