[Haskell-cafe] Default implementation of Floating (**)

Emilio Francesquini francesquini at gmail.com
Wed May 19 17:19:22 UTC 2021


The issue here, I guess, is that this behavior is inconsistent.

While the default implementation for (**) would produce NaNs and such, the
implementation for Float and Double overrides this behavior:

> (-3 :: Float) ** (-5)
-4.115226e-3
> (-3 :: Double) ** (-5)
-4.11522633744856e-3

Which is in accordance to what other languages such as C and Python do (and
also to what I would expect):

C
float b = -3.0;
float e = -5.0;
printf("%f\n", powf(b, e));

-0.004115

python
>>> -3.0 ** -5.0
-0.00411522633744856

The Haskell Report is not clear (or at least I was not able to find a
clearer explanation) about this:

6.4.3 Exponentiation and Logarithms
The one-argument exponential function exp and the logarithm function log
act on floating-point numbers and use base e. logBase a x returns the
logarithm of x in base a. sqrt returns the principal square root of a
floating-point number. There are three two-argument exponentiation
operations: (^) raises any number to a nonnegative integer power, (^^)
raises a fractional number to any integer power, and (⋆⋆)takes two
floating-point arguments. The value of x^0 or x^^0 is 1 for any x,
including zero; 0⋆⋆y is 1 if y is 1, and 0 otherwise.

Regards,

Emilio

On Wed, May 19, 2021 at 12:29 PM Henning Thielemann <
lemming at henning-thielemann.de> wrote:

>
> On Wed, 19 May 2021, Viktor Dukhovni wrote:
>
> > On Wed, May 19, 2021 at 11:50:22AM -0300, Fabrício Olivetti de França
> wrote:
> >
> >> So I was wondering, is there any reason to have this (potentially
> >> dangerous) default implementation on base?
> >
> > The behaviour for negative inputs seems to be correct:
> >
> >    λ> log(-2)
> >    NaN
> >    λ> exp(log(-2))
> >    NaN
> >    λ> exp(log(-2) * (-2))
> >    NaN
> >    λ> log(0)
> >    -Infinity
> >
> > were you expecting these to check for non-positive inputs and to throw a
> > floating point exception?
>
>
> I guess he expects (-2)**(-2) = 1/4.
>
> But defined this way, (**) would be rather
> discontinuous._______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20210519/8ea973d2/attachment.html>


More information about the Haskell-Cafe mailing list