Traditional awk implementations do not provide an exponent operator, so we define a function to calculate the exponent.
This one-liner reads base and exponent from stdin, one pair per line, and writes the result to stdout:

In C# it is possible to overload operators (+, -, *, etc..), but to do so requires the overload to implement at least one argument as the calling type.

What this means, is that if we have the class, A, to do an overload of + - we must set one of the arguments as the type "A".
This is because in C#, overloads are defined on a class basis - so when doing an operator, .Net looks at the class to find the operators. In this manner, one of the arguments must be of the class, else .Net would be looking there in vain.

This again means, that a direct overloading of the ^-character between two integers / double and integer is not possible.

However - coming to think of it, one could overload the "int" class, and enter the operator there.
--LordMike 17:45, 5 May 2010 (UTC)

While C++ does allow operator overloading, it does not have an exponentiation operator, therefore only a function definition is given. For non-negative exponents the integer and floating point versions are exactly the same, for obvious reasons. For negative exponents, the integer exponentiation would not give integer results; therefore there are several possibilities:

Use floating point results even for integer exponents.

Use integer results for integer exponents and give an error for negative exponents.

Use integer results for integer exponents and return just the integer part (i.e. return 0 if the base is larger than one and the exponent is negative).

The third option somewhat resembles the integer division rules, and has the nice property that it can use the exact same algorithm as the floating point version. Therefore this option is chosen here. Actually the template can be used with any type which supports multiplication, division and explicit initialization from int. Note that there are several aspects about int which are not portably defined; most notably it is not guaranteed

that the negative of a valid int is again a valid int; indeed for most implementations, the minimal value doesn't have a positive counterpart,

whether the result of a%b is positive or negative if a is negative, and in which direction the corresponding division is rounded (however, it is guaranteed that (a/b)*b + a%b == a)

The code below tries to avoid those platform dependencies. Note that bitwise operations wouldn't help here either, because the representation of negative numbers can vary as well.

Operators in Clojure are functions, so this satisfies both requirements. Also, this is polymorphic- it will work with integers, floats, etc, even ratios. (Since operators are implemented as functions they are used in prefix notation)

Common Lisp has a few forms of iteration. One of the more general is the do loop. Using the do loop, one definition is given below:

(defun my-expt-do(a b)(do((x 1(* x a))(y 0(+ y 1)))((= y b) x)))

do takes three forms. The first is a list of variable initializers and incrementers. In this case, x, the eventual return value, is initialized to 1, and every iteration of the do loop replaces the value of x with x * a. Similarly, y is initialized to 0 and is replaced with y + 1. The second is a list of conditions and return values. In this case, when y = b, the loop stops, and the current value of x is returned. Common Lisp has no explicit return keyword, so x ends up being the return value for the function. The last form is the body of the loop, and usually consists of some action to perform (that has some side-effect). In this case, all the work is being done by the first and second forms, so there are no extra actions.

This solution uses the fact that a^0 = 1 and that a^b = a * a^{b-1}. cond is essentially a generalized if-statement. It takes a list of forms of the form (condresult). For instance, in this case, if b = 0, then function returns 1. t is the truth constant in Common Lisp and is often used as a default condition (similar to the default keyword in C/C++/Java or the else block in many languages).

Common Lisp has much more lenient rules for identifiers. In particular, ^ is a valid CL identifier. Since it is not already defined in the standard library, we can simply use it as a function name, just like any other function.

We have a case of true polymorphism here and no overloading is required. However Ela supports overloading
using classes (somewhat similar to Haskell type classes) so we can show how the same implementation could
work with overloading (less preferable in this case because of more redundant code but still possible):

There's no difference in Haskell between a procedure (or function) and an operator, other than the infix notation. This routine is overloaded for any integral exponent (which includes the arbitrarily large Integer type) and any numeric type for the bases (including, for example, Complex). It uses the fast "binary" exponentiation algorithm. For a negative exponent, the type of the base must support division (and hence reciprocals):

The procedure below will take an integer or real base and integer exponent and return base ^ exponent. If exponent is negative, base is coerced to real so as not to return 0. Operator overloading is not supported and this is not an efficient implementation.

J is concretely specified, which makes it easy to define primitives in terms of other primitives (this is especially true of mathematical primitives, given the language's mathematical emphasis).

So we have any number of options. Here's the simplest, equivalent to the for each number, product = product * number of other languages. The base may be any number, and the exponent may be any non-negative integer (including zero):

exp =: */@:#~

10 exp 31000

10 exp 01

We can make this more general by allowing the exponent to be any integer (including negatives), at the cost of a slight increase in complexity:

exp =: *@:] %: */@:(#~|)

10 exp _30.001

Or, we can define exponentiation as repeated multiplication (as opposed to multiplying a given number of copies of the base)

exp =: dyad def 'x *^:y 1'

10 exp 3100010 exp _30.001

Here, when we specify a negative number of repetitions, multiplication's inverse is used that many times.

J's calculus of functions permits us to define exponentiation in its full generality, as the inverse of log (i.e. exp = log-1):

exp =: ^.^:_1

81 exp 0.59

Note that the definition above does not use the primitive exponentiation function ^ . The carets in it represent different (but related) things . The function is composed of three parts: ^. ^: _1 . The first part, ^., is the primitive logarithm operator (e.g. 3 = 10^.1000) .

The second part, ^: , is interesting: it is a "meta operator". It takes two arguments: a function f on its left, and a number N on its right. It produces a new function, which, when given an argument, applies f to that argument N times. For example, if we had a function increment, then increment^:3 X would increment X three times, so the result would be X+3.

In the case of ^. ^: _1 , f is ^. (i.e. logarithm) and N is -1. Therefore we apply log negative one times or the inverse of log once (precisely as in log-1).

Similarly, we can define exponentiation as the reverse of the inverse of root. That is, x pow y = y root-1 x:

exp =: %:^:_1~

81 exp 0.59

Compare this with the previous definition: it is the same, except that %: , root, has been substituted for ^. , logarithm, and the arguments have been reversed (or reflected) with ~.

That is, J is telling us that power is the same as the reflex of the inverse of root, exactly as we'd expect.

One last note: we said these definitions are the same as ^ in its full generality. What is meant by that? Well, in the context of this puzzle, it means both the base and exponent may be any real number. But J goes further than that: it also permits complex numbers.

Kotlin does not have a dedicated exponentiation operator (we would normally use Java's Math.pow method instead) but it's possible to implement integer and floating power exponentiation (with integer exponents) using infix extension functions which look like non-symbolic operators for these actions:

Whilst some implementations or dialects of Modula-2 may permit definition or overloading of operators, neither is permitted in N.Wirth's classic language definition and the ISO Modula-2 standard. The operations are therefore given as library functions.

(* Library Interface *)DEFINITIONMODULE Exponentiation;

PROCEDURE IntExp(base, exp :INTEGER):INTEGER;(* Raises base to the power of exp and returns the result both base and exp must be of type INTEGER *)

PROCEDURE RealExp(base :REAL; exp :INTEGER):REAL;(* Raises base to the power of exp and returns the result base must be of type REAL, exp of type INTEGER *)

sub expon {my($base,$expo)=@_;if($expo==0){return1;}elsif($expo==1){return$base;}elsif($expo>1){my$prod=1;foreachmy$n(0..($expo-1)){$prod*=$base;}return$prod;}elsif($expo<0){return1/( expon ($base,-$expo));}}print"3 to the power of 10 as a function is ". expon(3,10)." !\n";print"3 to the power of 10 as a builtin is ".3**10." !\n";print"5.5 to the power of -3 as a function is ". expon(5.5,-3)." !\n";print"5.5 to the power of -3 as a builtin is ".5.5**-3." !\n";

Output:

3 to the power of 10 as a function is 59049 !
3 to the power of 10 as a builtin is 59049 !
5.5 to the power of -3 as a function is 0.00601051840721262 !
5.5 to the power of -3 as a builtin is 0.00601051840721262 !

The following version is simpler and much faster for large exponents, since it uses exponentiation by squaring.

sub ex {my($base,$exp)=@_;die"Exponent '$exp' must be an integer!"if$exp!=int($exp);return1if$exp==0;($base,$exp)=(1/$base,-$exp)if$exp<0;my$c=1;while($exp>1){$c*=$baseif$exp%2;$base*=$base;$exp>>=1;}$base*$c;}

The builtin power function handles atoms and integers for both arguments, whereas this deliberately restricts the exponent to an integer.
There is no operator overloading in Phix, or for that matter any builtin overriding.

The negative first argument needs to be put in parentheses because it would otherwise be passed as string.
This can be circumvented by declaring the first argument to the function as double, but then the return type would be always double while currently pow 2 3 returns an int.

In Prolog, we define predicates rather than functions. Still, functions and predicates are related: going one way, we can think of an n-place predicate as a function from its arguments to a member of the set {true, false}; going the other way, we can think of functions as predicates with a hidden ultimate argument, called a "return value". Following the latter approach, Prolog sometimes uses macro expansion to provide functional syntax by

1. catching terms fitting a certain pattern (viz. Base ^^ Exp, which is the same as '^^'(N, 3)),

2. calling the term with an extra argument (viz. call('^^'(Base, Exp), Power)),

3. replacing the occurrence of the term with the value instantiated in the extra argument (viz. Power).

The predicate is/2 supports functional syntax in its second argument: e.g., X is sqrt(2) + 1. New arithmetic functions can be added with the `arithmetic_function/1` directive, wherein the arity attributed to the function is one less than the arity of the predicate which will be called during term expansion and evaluation. The following directives establish ^^/2 as, first, an arithmetic function, and then as a right-associative binary operator (so that X is 2^^2^^2 == X = 2^(2^2)</code>):

This solution employs the higher-order predicate foldl/4 from the standard SWI-Prolog library(apply), in conjunction with an auxiliary "folding predicate" (note, the definition uses the ^^ operator as an arithmetic function):

%% ^^/3%% True if Power is Base ^ Exp.

^^(Base,Exp, Power):-(Exp<0-> Power is1/(Base ^^ (Exp*-1))% If exponent is negative, then ...

;Exp>0-> length(Powers,Exp),% If exponent is positive, then foldl( exp_folder(Base), Powers,1, Power )% Powers is a list of free variables with length Exp% and Power is Powers folded with exp_folder/4

; Power =1% otherwise Exp must be 0, so).

%% exp_folder/4%% True when Power is the product of Base and Powers.% % This predicate is designed to work with foldl and a list of free variables.% It passes the result of each evaluation to the next application through its% fourth argument, instantiating the elements of Powers to each successive Power of the Base.

The num crate is the de-facto Rust library for numerical generics and it provides the One trait which allows for an exponentiation function that is generic over both integral and floating point types. The library provides this generic exponentiation function, the implementation of which is the pow function below.

There's no distinction between an operator and a method in Scala. Alas, there is no way of adding methods to a class, but one can make it look like a method has been added, through a method commonly known as Pimp My Library. Therefore, we show below how that can beaccomplished. We define the operator ↑ (unicode's uparrow), which is written as \u2191 below, to make cut & paste easier.

To use it, one has to import the implicit from the appropriate object. ExponentI will work for any integral type (Int, BigInt, etc), ExponentF will work for any fractional type (Double, BigDecimal, etc). Importing both at the same time won't work. In this case, it might be better to define implicits for the actual types being used, such as was done in Exponents.

This definition of the exponentiation procedure ^ operates on bases of all numerical types that the multiplication procedure * operates on, i. e. integer, rational, real, and complex. The notion of an operator does not exist in Scheme. Application of a procedure to its arguments is always expressed with a prefix notation.

In Seed7 the ** operator is overloaded for both
integerinteger
and floatinteger
(additionally there is a ** operator
for floatfloat).
The following re-implementation of both functions does not use another exponentiation
function to do the computation.
Instead the exponentiation-by-squaring algorithm is used.

const func float: fltIPow (in var float: base, in var integer: exponent) is func result var float: power is 1.0; local var integer: stop is 0; begin if base = 0.0 then if exponent < 0 then power := Infinity; elsif exponent > 0 then power := 0.0; end if; else if exponent < 0 then stop := -1; end if; if odd(exponent) then power := base; end if; exponent >>:= 1; while exponent <> stop do base *:= base; if odd(exponent) then power *:= base; end if; exponent >>:= 1; end while; if stop = -1 then power := 1.0 / power; end if; end if; end func;

Tcl already has both an exponentiation function (set x [expr {pow(2.4, 3.5)}]) and operator (set x [expr {2.4 ** 3.5}]). The operator cannot be overridden. The function may be overridden by a procedure in the tcl::mathfunc namespace, relative to
the calling namespace.

ZX Spectrum Basic does not support custom operators or integer datatypes, but here we implement exponentation using a function.
The function itself makes use of the inbuilt exponentiation operator, which is kind of cheating, but hey this provides a working implementation.