Exponentiation with infix operators in (or operating on) the base: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 66: Line 66:
The order of precedence for Ada operators is:
The order of precedence for Ada operators is:
<pre>
<pre>
logical_operator ::= and | or | xor
logical_operator ::= and | or | xor
relational_operator ::= = | /= | < | <= | > | >=
relational_operator ::= = | /= | < | <= | > | >=
binary_adding_operator ::= + | – | &
binary_adding_operator ::= + | – | &
unary_adding_operator ::= + | –
unary_adding_operator ::= + | –
multiplying_operator ::= * | / | mod | rem
multiplying_operator ::= * | / | mod | rem
highest_precedence_operator ::= ** | abs | not
highest_precedence_operator ::= ** | abs | not
</pre>
</pre>
Ada provides an exponentiation operator for integer types and floating point types.
Ada provides an exponentiation operator for integer types and floating point types.
<lang Ada>
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 89: Line 88:
Put("x =" & ivalue'image & " p =" & power'image);
Put("x =" & ivalue'image & " p =" & power'image);
Put(" -x ** p ");
Put(" -x ** p ");
Put(item => ivalue ** power, width => 4);
Put(item => -ivalue ** power, width => 4);
Put(" -(x) ** p ");
Put(" -(x) ** p ");
Put(item => -(ivalue) ** power, width => 4);
Put(item => -(ivalue) ** power, width => 4);
Put(" (-x) ** p ");
Put(" (-x) ** p ");
Put(Item => (- ivalue) ** power, Width => 4);
Put(Item => (- ivalue) ** power, Width => 4);
Put(" -(x ** p) ");
Put(" -(x ** p) ");
Line 104: Line 103:
ivalue := -5;
ivalue := -5;
fvalue := -5.0;
fvalue := -5.0;

for i in 1..2 loop
for i in 1..2 loop
for power in 2..3 loop
for power in 2..3 loop
Put("x =" & ivalue'image & " p =" & power'image);
Put("x =" & fvalue'image & " p =" & power'image);
Put(" -x ** p ");
Put(" -x ** p ");
Put(item => fvalue ** power, fore => 4, Aft => 1, Exp => 0);
Put(item => -fvalue ** power, fore => 4, Aft => 1, Exp => 0);
Put(" -(x) ** p ");
Put(" -(x) ** p ");
Put(item => -(fvalue) ** power, fore => 4, Aft => 1, Exp => 0);
Put(item => -(fvalue) ** power, fore => 4, Aft => 1, Exp => 0);
Put(" (-x) ** p ");
Put(" (-x) ** p ");
Put(Item => (- fvalue) ** power, fore => 4, Aft => 1, Exp => 0);
Put(Item => (- fvalue) ** power, fore => 4, Aft => 1, Exp => 0);
Put(" -(x ** p) ");
Put(" -(x ** p) ");
Line 122: Line 121:
end loop;
end loop;
end Main;
end Main;

</lang>
</lang>
{{output}}
{{output}}
<pre>
<pre>
Integer exponentiation:
Integer exponentiation:
x =-5 p = 2 -x ** p 25 -(x) ** p -25 (-x) ** p 25 -(x ** p) -25
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 = 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 = 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 = 3 -x ** p -125 -(x) ** p -125 (-x) ** p -125 -(x ** p) -125
floating point exponentiation:
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.00000E+00 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.00000E+00 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.00000E+00 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.00000E+00 p = 3 -x ** p -125.0 -(x) ** p -125.0 (-x) ** p -125.0 -(x ** p) -125.0
</pre>
</pre>