Most programming languages have a built-in implementation of exponentiation.



Task

Re-implement integer exponentiation for both   intint   and   floatint   as both a procedure,   and an operator (if your language supports operator definition).

If the language supports operator (or procedure) overloading, then an overloaded form should be provided for both   intint   and   floatint   variants.



Related tasks

   Exponentiation order
   arbitrary-precision integers (included)
   Exponentiation with infix operators in (or operating on) the base


' FB 1.05.0
 
' Note that 'base' is a keyword in FB, so we use 'base_' instead as a parameter
 
Function Pow Overload (base_ As Double, exponent As Integer) As Double
  If exponent = 0.0 Then Return 1.0
  If exponent = 1.0 Then Return base_
  If exponent < 0.0 Then Return 1.0 / Pow(base_, -exponent)
  Dim power As Double = base_
  For i As Integer = 2 To exponent
     power *= base_
  Next
  Return power
End Function
 
Function Pow Overload(base_ As Integer, exponent As Integer) As Double
   Return Pow(CDbl(base_), exponent)
End Function
 
' check results of these functions using FB's built in '^' operator
Print "Pow(2, 2)       = "; Pow(2, 2)
Print "Pow(2.5, 2)     = "; Pow(2.5, 2)
Print "Pow(2, -3)      = "; Pow(2, -3) 
Print "Pow(1.78, 3)    = "; Pow(1.78, 3)
Print
Print "2 ^ 2           = "; 2 ^ 2
Print "2.5 ^ 2         = "; 2.5 ^ 2
Print "2 ^ -3          = "; 2 ^ -3 
Print "1.78 ^ 3        = "; 1.78 ^ 3
Print
Print "Press any key to quit"
Sleep