Haskell 98 - Standard Prelude - Floating Class
Mon, 15 Oct 2001 18:27:52 +0200
Simon PJ wrote:
> Fpr the Revised Haskell 98 report, Russell O'Connor suggests:
> | Also, I understand you are reluctant to make library changes,=20
> | but sinh and cosh can easily be defined in terms of exp
> | sinh x =3D (exp(x) - exp(-x))/2
> | cosh x =3D (exp(x) + exp(-x))/2
> | (source: Calculus Third Edition by Michael Spivak, page 349,=20
> | or any decent calculus book)
> | 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.
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.
I suggest saying as little about the transcendental functions as possible, rather
than forcing incorrect rules on the implementor. I imagine most Haskell implementors
do not want to waste time writing their own transcendental function routines,
but simply call out to one of the various high-quality C implementations. (Netlib
has a good free one, for example.) This will probably produce better results than
suggesting buggy code to the implementor.
Technically, you would probably be able to define all the functions sinh/cosh/tanh/exp
functions with reasonable precision in terms of expm1, defined mathematically as
expm1(x) = exp(x) - 1.
But please don't . . .