Jump to content

Exponentiation operator: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎{{header|Kotlin}}: replaced modulo 2 with corresponding bit operation for consistency and improved efficiency slightly)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 9:
 
If the language supports operator (or procedure) overloading, then an overloaded form should be provided for both &nbsp; <big>int<sup>int</sup></big> &nbsp; and &nbsp; <big>float<sup>int</sup></big> &nbsp; variants.
<br><br>
 
=={{header|Ada}}==
Line 875:
14348907
</pre>
 
=={{header|Factor}}==
Simple, unoptimized implementation which accepts a positive or negative exponent:
Line 1,527 ⟶ 1,528:
result y asa k eq 0;
end</lang>
 
=={{header|M2000 Interpreter}}==
<lang M2000 Interpreter>
Line 2,025 ⟶ 2,027:
$base * $c;
}</lang>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2018.03}}
 
<lang perl6>subset Natural of Int where { $^n >= 0 }
 
multi pow (0, 0) { fail '0**0 is undefined' }
multi pow ($base, Natural $exp) { [*] $base xx $exp }
multi pow ($base, Int $exp) { 1 / pow $base, -$exp }
 
sub infix:<***> ($a, $b) { pow $a, $b }
 
# Testing
 
say pow .75, -5;
say .75 *** -5;</lang>
 
=={{header|Phix}}==
Line 2,137 ⟶ 2,123:
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 <code>double</code>, but then the return type would be always double while currently <code>pow 2 3</code> returns an <code>int</code>.
 
=={{header|PureBasic}}==
PureBasic does not allow an operator to be redefined or operator overloading.
<lang PureBasic>Procedure powI(base, exponent)
Protected i, result.d
If exponent < 0
If base = 1
result = 1
EndIf
ProcedureReturn result
EndIf
result = 1
For i = 1 To exponent
result * base
Next
ProcedureReturn result
EndProcedure
Procedure.f powF(base.f, exponent)
Protected i, magExponent = Abs(exponent), result.d
If base <> 0
result = 1.0
If exponent <> 0
For i = 1 To magExponent
result * base
Next
If exponent < 0
result = 1.0 / result
EndIf
EndIf
EndIf
ProcedureReturn result
EndProcedure
 
If OpenConsole()
Define x, a.f, exp
x = Random(10) - 5
a = Random(10000) / 10000 * 10
For exp = -3 To 3
PrintN(Str(x) + " ^ " + Str(exp) + " = " + Str(powI(x, exp)))
PrintN(StrF(a) + " ^ " + Str(exp) + " = " + StrF(powF(a, exp)))
PrintN("--------------")
Next
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf</lang>
{{out}}
<pre>-3 ^ -3 = 0
6.997000 ^ -3 = 0.002919
--------------
-3 ^ -2 = 0
6.997000 ^ -2 = 0.020426
--------------
-3 ^ -1 = 0
6.997000 ^ -1 = 0.142918
--------------
-3 ^ 0 = 1
6.997000 ^ 0 = 1.000000
--------------
-3 ^ 1 = -3
6.997000 ^ 1 = 6.997000
--------------
-3 ^ 2 = 9
6.997000 ^ 2 = 48.958012
--------------
-3 ^ 3 = -27
6.997000 ^ 3 = 342.559235
-------------</pre>
 
 
=={{header|Prolog}}==
Line 2,293 ⟶ 2,207:
NewExp is Exp - 1,
exp_recursive_(Base, NewExp, NewAcc, Power).</lang>
 
=={{header|PureBasic}}==
PureBasic does not allow an operator to be redefined or operator overloading.
<lang PureBasic>Procedure powI(base, exponent)
Protected i, result.d
If exponent < 0
If base = 1
result = 1
EndIf
ProcedureReturn result
EndIf
result = 1
For i = 1 To exponent
result * base
Next
ProcedureReturn result
EndProcedure
Procedure.f powF(base.f, exponent)
Protected i, magExponent = Abs(exponent), result.d
If base <> 0
result = 1.0
If exponent <> 0
For i = 1 To magExponent
result * base
Next
If exponent < 0
result = 1.0 / result
EndIf
EndIf
EndIf
ProcedureReturn result
EndProcedure
 
If OpenConsole()
Define x, a.f, exp
x = Random(10) - 5
a = Random(10000) / 10000 * 10
For exp = -3 To 3
PrintN(Str(x) + " ^ " + Str(exp) + " = " + Str(powI(x, exp)))
PrintN(StrF(a) + " ^ " + Str(exp) + " = " + StrF(powF(a, exp)))
PrintN("--------------")
Next
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf</lang>
{{out}}
<pre>-3 ^ -3 = 0
6.997000 ^ -3 = 0.002919
--------------
-3 ^ -2 = 0
6.997000 ^ -2 = 0.020426
--------------
-3 ^ -1 = 0
6.997000 ^ -1 = 0.142918
--------------
-3 ^ 0 = 1
6.997000 ^ 0 = 1.000000
--------------
-3 ^ 1 = -3
6.997000 ^ 1 = 6.997000
--------------
-3 ^ 2 = 9
6.997000 ^ 2 = 48.958012
--------------
-3 ^ 3 = -27
6.997000 ^ 3 = 342.559235
-------------</pre>
 
=={{header|Python}}==
Line 2,334 ⟶ 2,319:
(^ 5 2) ; 25
(^ 5.0 2) ; 25.0</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.03}}
 
<lang perl6>subset Natural of Int where { $^n >= 0 }
 
multi pow (0, 0) { fail '0**0 is undefined' }
multi pow ($base, Natural $exp) { [*] $base xx $exp }
multi pow ($base, Int $exp) { 1 / pow $base, -$exp }
 
sub infix:<***> ($a, $b) { pow $a, $b }
 
# Testing
 
say pow .75, -5;
say .75 *** -5;</lang>
 
=={{header|Retro}}==
Line 2,535 ⟶ 2,537:
2^0 = 1
-2^0 = 1</pre>
 
=={{header|Rust}}==
The <code>num</code> crate is the de-facto Rust library for numerical generics and it provides the <code>One</code> 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 <code>pow</code> function below.
Line 2,797 ⟶ 2,800:
(x ln * y) exp
].</lang>
 
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
Line 2,989 ⟶ 2,993:
(-3.0)^(-4)= 1,23456790123457E-02
0.0^(-4)= Fout 11</pre>
 
=={{header|VBScript}}==
<lang vb>
10,339

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.