[Haskell-cafe] How odd...
Andrew Coppin
andrewcoppin at btinternet.com
Sat Aug 4 11:38:08 EDT 2007
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...
More information about the Haskell-Cafe
mailing list