Exponentiation with infix operators in (or operating on) the base: Difference between revisions
Content added Content deleted
m (used a smaller fontsize.) |
No edit summary |
||
Line 62: | Line 62: | ||
* [[wp:Order_of_operations#Programming_languages|Wikipedia: Order of operations in Programming languages]] |
* [[wp:Order_of_operations#Programming_languages|Wikipedia: Order of operations in Programming languages]] |
||
<br><br> |
<br><br> |
||
=={{header|Ada}}== |
|||
The order of precedence for Ada operators is: |
|||
<pre> |
|||
logical_operator ::= and | or | xor |
|||
relational_operator ::= = | /= | < | <= | > | >= |
|||
binary_adding_operator ::= + | – | & |
|||
unary_adding_operator ::= + | – |
|||
multiplying_operator ::= * | / | mod | rem |
|||
highest_precedence_operator ::= ** | abs | not |
|||
</pre> |
|||
Ada provides an exponentiation operator for integer types and floating point types. |
|||
<lang Ada> |
|||
with Ada.Text_IO; use Ada.Text_IO; |
|||
with Ada.Float_Text_IO; use Ada.Float_Text_IO; |
|||
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; |
|||
procedure Main is |
|||
ivalue : Integer := -5; |
|||
fvalue : float := -5.0; |
|||
begin |
|||
Put_Line("Integer exponentiation:"); |
|||
for i in 1..2 loop |
|||
for power in 2..3 loop |
|||
Put("x =" & ivalue'image & " p =" & power'image); |
|||
Put(" -x ** p "); |
|||
Put(item => ivalue ** power, width => 4); |
|||
Put(" -(x) ** p "); |
|||
Put(item => -(ivalue) ** power, width => 4); |
|||
Put(" (-x) ** p "); |
|||
Put(Item => (- ivalue) ** power, Width => 4); |
|||
Put(" -(x ** p) "); |
|||
Put(Item => -(ivalue ** power), Width => 4); |
|||
New_line; |
|||
end loop; |
|||
ivalue := 5; |
|||
fvalue := 5.0; |
|||
end loop; |
|||
Put_Line("floating point exponentiation:"); |
|||
ivalue := -5; |
|||
fvalue := -5.0; |
|||
for i in 1..2 loop |
|||
for power in 2..3 loop |
|||
Put("x =" & ivalue'image & " p =" & power'image); |
|||
Put(" -x ** p "); |
|||
Put(item => fvalue ** power, fore => 4, Aft => 1, Exp => 0); |
|||
Put(" -(x) ** p "); |
|||
Put(item => -(fvalue) ** power, fore => 4, Aft => 1, Exp => 0); |
|||
Put(" (-x) ** p "); |
|||
Put(Item => (- fvalue) ** power, fore => 4, Aft => 1, Exp => 0); |
|||
Put(" -(x ** p) "); |
|||
Put(Item => -(fvalue ** power), fore => 4, Aft => 1, Exp => 0); |
|||
New_line; |
|||
end loop; |
|||
ivalue := 5; |
|||
fvalue := 5.0; |
|||
end loop; |
|||
end Main; |
|||
</lang> |
|||
{{output}} |
|||
<pre> |
|||
Integer exponentiation: |
|||
x =-5 p = 2 -x ** p 25 -(x) ** p -25 (-x) ** p 25 -(x ** p) -25 |
|||
x =-5 p = 3 -x ** p -125 -(x) ** p 125 (-x) ** p 125 -(x ** p) 125 |
|||
x = 5 p = 2 -x ** p 25 -(x) ** p -25 (-x) ** p 25 -(x ** p) -25 |
|||
x = 5 p = 3 -x ** p 125 -(x) ** p -125 (-x) ** p -125 -(x ** p) -125 |
|||
floating point exponentiation: |
|||
x =-5 p = 2 -x ** p 25.0 -(x) ** p -25.0 (-x) ** p 25.0 -(x ** p) -25.0 |
|||
x =-5 p = 3 -x ** p -125.0 -(x) ** p 125.0 (-x) ** p 125.0 -(x ** p) 125.0 |
|||
x = 5 p = 2 -x ** p 25.0 -(x) ** p -25.0 (-x) ** p 25.0 -(x ** p) -25.0 |
|||
x = 5 p = 3 -x ** p 125.0 -(x) ** p -125.0 (-x) ** p -125.0 -(x ** p) -125.0 |
|||
</pre> |
|||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
In Algol 68, all unary operators have a higher precedence than any binary operator. |
In Algol 68, all unary operators have a higher precedence than any binary operator. |