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

no edit summary
No edit summary
No edit summary
Line 66:
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.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;
Line 89 ⟶ 88:
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) ");
Line 104 ⟶ 103:
ivalue := -5;
fvalue := -5.0;
 
for i in 1..2 loop
for power in 2..3 loop
Put("x =" & ivaluefvalue'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) ");
Line 122 ⟶ 121:
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.00000E+00 p = 2 -x ** p -25.0 -(x) ** p -25.0 (-x) ** p 25.0 -(x ** p) -25.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.00000E+00 p = 2 -x ** p -25.0 -(x) ** p -25.0 (-x) ** p 25.0 -(x ** p) -25.0
x = 5.00000E+00 p = 3 -x ** p -125.0 -(x) ** p -125.0 (-x) ** p -125.0 -(x ** p) -125.0
</pre>
 
82

edits