# Power function

### From HaskellWiki

## Contents |

## 1 Question

Why are there several notions of power in Haskell, namely-- typically for integers (^) :: (Num a, Integral b) => a -> b -> a -- typically for rationals (^^) :: (Fractional a, Integral b) => a -> b -> a -- typically for floating-point numbers (**) :: Floating a => a -> a -> a

## 2 Answer

The reason is that there is no implementation of the power function that can cover all exotic choices for basis and exponent while being both efficient and accurate.

See this StackOverflow question for details.

### 2.1 Type inference reasons

In mathematical notation, the human reader is clever enough to to tell which definition of the power function is applicable in a given context. In Haskell, doing so would drastically complicate type inference. In some other languages such as C++, operator overloading is used to work around this problem, but this approach does not work for Haskell's numeric type classes.

### 2.2 Mathematical reasons

If we assume the most general types for both basis and exponent, namely complex numbers, the result of the power is no longer unique. In particular, all possible solutions of say 1^{x}, where *x* is irrational, is dense in the complex unit circle.

^{15}th root of −1 does not exist. Fortunately, the Haskell implementation does not try to be too clever here. But it does so at another point:

Prelude> (-1)**2 :: Double 1.0 Prelude> (-1)**(2 + 1e-15 - 1e-15) :: Double NaN

## 3 Power function in Numeric Prelude

One can refine the set of power functions further as it is done in the Numeric Prelude. In this library, the more general the basis the less general the exponent and vice versa:

basis type | provides | symbol | exponent type | definition | |

any ring | (*) |
(^) |
cardinal | repeated multiplication | |

any field | (/) |
(^-) |
integer | multiplication and division | |

an algebraic field | root |
(^/) |
rational | list of polynomial zeros (length = denominator of the exponent) | |

positive real | log |
(^?) |
any ring of characteristic zero with inverses for integers and a notion of limit | exponential series and logarithm |

- examples for rings are: Polynomials, Matrices, Residue classes
- examples for fields: Fractions of polynomials (rational functions), Residue classes with respect to irreducible divisors, in fact we do not need fields, we only need the division and associativity, thus invertible Matrices are fine

## 4 See also

- Haskell-Cafe: Proposal for restructuring Number classes