[Haskell-cafe] Fortran mixed mode arithmetic expressions -> Haskell

Daniel Fischer daniel.is.fischer at web.de
Mon Oct 26 13:09:56 EDT 2009


Am Monday 26 October 2009 17:24:46 schrieben Sie:
> Being new to Haskell, I take it (^) and (^^) would be the preferred
> exponential "operator." When (how,where,why) would one use (**)?

The beasts have different types and are for different things:
Prelude> :i (^)
(^) :: (Num a, Integral b) => a -> b -> a       -- Defined in GHC.Real
infixr 8 ^

This one raises any number to a nonnegative integral power.
A typical implementation would be power by repeated squaring.

Prelude> :i (^^)
(^^) :: (Fractional a, Integral b) => a -> b -> a
        -- Defined in GHC.Real
infixr 8 ^^

This one allows also negative powers, so the type of base must allow inversion, hence it 
must belong to Fractional. The exponent must still be an integer, you can't use this for 
n-th roots or similar.
A typical implementation would be power by repeated squaring, followed by (1/) if the 
exponent is negative.

Prelude> :i (**)
class (Fractional a) => Floating a where
  ...
  (**) :: a -> a -> a
  ...
        -- Defined in GHC.Float
infixr 8 **

This one raises a floating point number to an arbitrary power, so you can use it for n-th 
roots.
A typical implementation would be
b ** e = exp (e*log b).



More information about the Haskell-Cafe mailing list