[Haskell-cafe] How odd...

Paul Johnson paul at cogito.org.uk
Sat Aug 4 11:01:40 EDT 2007


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)

So 0 ** 2 is equivalent to exp (log 0 * 2)

 > log 0
-Infinity

 > log 0 * 2
-Infinity

 > exp (log 0 * 2)
0.0

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.

So no, its not a bug, its according to the standard.  Whether the 
standard ought to be modified for multiplication by numbers of the form 
(x :+ 0) is another question.  Arguably the correct value in the last 
case should have been (-Infinity) :+ 0.0 on the grounds that (x :+ y) * 
2 should be equal to (x * 2 :+ y * 2).  In most cases this holds, but if 
one of the complex multiplicands contains an infinity then you get a NaN 
instead.

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.

Paul.



More information about the Haskell-Cafe mailing list