Power function
Question[edit]
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
Answer[edit]
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.
Type inference reasons[edit]
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.
Mathematical reasons[edit]
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 1x, where x is irrational, is dense in the complex unit circle.
But even for real numbers there are problems: to calculate (-1)**(1/3::Double)
the power implementation would have to decide whether
(1/3::Double)
is close enough to ⅓.
If it does so it returns (-1)
, otherwise it fails. However, why shall 0.333333333333333
represent ⅓? It may be really meant as 333333333333333/10^15
, and a real 1015th 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
While both expressions should be evaluated to 1.0
, a reliable check for integers is not possible with floating-point numbers.
Power function in Numeric Prelude[edit]
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
That is, (^-)
replaces (^^)
,
(^?)
replaces (**)
,
(^)
remains and (^/)
is new.
See also[edit]
- Haskell-Cafe: Proposal for restructuring Number classes