[Haskell-cafe] How odd...
Lennart Augustsson
lennart at augustsson.net
Sat Aug 4 11:47:16 EDT 2007
Haskell doesn't know much about infinity, but Haskell implementations are
allowed to use IEEE floating point which has infinity.
And to get things right, there needs to be a few changes to the library to
do the right thing for certain numbers, this is not news. In fact I filed a
bug report a while back about it.
-- Lennart
On 8/4/07, Andrew Coppin <andrewcoppin at btinternet.com> wrote:
>
> Paul Johnson wrote:
> > Andrew Coppin wrote:
> >> > 0**2
> >> 0
> >>
> >> > (0 :+ 0)**2
> >> NaN :+ NaN
> >>
> >> (Is this a bug?)
> > According to the Standard Prelude,
> > # x ** y = exp (log x * y)
>
> I had a feeling this would be the cause.
>
> > > log 0
> > -Infinity
>
> Oh. So... since when does Haskell know about infinity?
>
> BTW, I recently had some code like this:
>
> foo x
> | x < 0 = ...
> | x == 0 = ...
> | x > 0 = ...
>
> I was most perplexed when I got a "non-exhaustive patterns" exception...
> It turns out there was a NaN in there. I forget about that.
>
> > > exp (log 0 * 2)
> > 0.0
>
> Well that's interesting. I did wonder why it *doesn't* break in the real
> case...
>
> > On to the complex number case. From the standard for Complex:
> >
> > # log z = log (magnitude z) :+ phase z
> >
> > # phase (0 :+ 0) = 0
> > This is a special case for the phase of zero.
> >
> > # (x:+y) * (x':+y') = (x*x'-y*y') :+ (x*y'+y*x')
> >
> > > log (0 :+ 0)
> > (-Infinity) :+ 0.0
> >
> > > log (0 :+ 0) * 2
> > (-Infinity) :+ NaN
> >
> > Which is the source of the problem. The imaginary part involves
> > multiplying (-Infinity) by the
> > imaginary part of 2 (i.e. 0), which is NaN.
>
> Um... why would infinity * 0 be NaN? That doesn't make sense...
>
> > So no, its not a bug, its according to the standard.
>
> So I'm the only person who was expecting zero squared to be zero? (IMHO
> the standard should try to implement mathematical operations in a
> mathematically sensible way...)
>
> > While working through this I also came across the following case which
> > technically is a bug:
> >
> > > 0 ** 0
> > 1.0
> >
> > > exp (log 0 * 0)
> > NaN
> >
> > I suspect that GHCi is using a built-in exponentiation operator that
> > doesn't quite conform to the standard in this case.
>
> Now that really *is* odd...
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070804/a614e418/attachment.htm
More information about the Haskell-Cafe
mailing list