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

m (→‎{{header|ALGOL 68}}: Remove unnecessary ;)
Line 389:
x is 5, p is 2, -x^p is 25, -(x)^p is 25, (-x)^p is 25, -(x^p) is -25
x is 5, p is 3, -x^p is -125, -(x)^p is -125, (-x)^p is -125, -(x^p) is -125</pre>
 
=={{header|Pascal}}==
{{works with|Extended Pascal}}
Apart from the built-in (prefix) functions <tt>sqr</tt> (exponent&nbsp;=&nbsp;2) and <tt>sqrt</tt> (exponent&nbsp;=&nbsp;0.5) already defined in Standard “Unextended” Pascal (ISO standard 7185), ''Extended Pascal'' (ISO standard 10206) defines following additional (infix) operators:
<lang pascal>program exponentiationWithInfixOperatorsInTheBase(output);
 
const
minimumWidth = 7;
fractionDigits = minimumWidth div 4 + 1;
 
procedure testIntegerPower(
{ `pow` can in fact accept `integer`, `real` and `complex`. }
protected base: integer;
{ For `pow` the `exponent` _has_ to be an `integer`. }
protected exponent: integer
);
begin
writeLn('=====> testIntegerPower <=====');
writeLn(' base = ', base:minimumWidth);
writeLn(' exponent = ', exponent:minimumWidth);
{ Note: `exponent` may not be negative if `base` is zero! }
writeLn(' -base pow exponent = ', -base pow exponent:minimumWidth);
writeLn('-(base) pow exponent = ', -(base) pow exponent:minimumWidth);
writeLn('(-base) pow exponent = ', (-base) pow exponent:minimumWidth);
writeLn('-(base pow exponent) = ', -(base pow exponent):minimumWidth)
end;
 
procedure testRealPower(
{ `**` actually accepts all data types (`integer`, `real`, `complex`). }
protected base: real;
{ The `exponent` in an `**` expression will be, if applicable, }
{ _promoted_ to a `real` value approximation. }
protected exponent: integer
);
begin
writeLn('======> testRealPower <======');
writeLn(' base = ', base:minimumWidth:fractionDigits);
writeLn(' exponent = ', exponent:pred(minimumWidth, succ(fractionDigits)));
if base > 0.0 then
begin
{ The result of `base ** exponent` is a `complex` value }
{ `base` is a `complex` value, `real` otherwise. }
writeLn(' -base ** exponent = ', -base ** exponent:minimumWidth:fractionDigits);
writeLn('-(base) ** exponent = ', -(base) ** exponent:minimumWidth:fractionDigits);
writeLn('(-base) ** exponent = illegal');
writeLn('-(base ** exponent) = ', -(base ** exponent):minimumWidth:fractionDigits)
end
else
begin
{ “negative” zero will not alter the sign of the value. }
writeLn(' -base ** exponent = ', -base pow exponent:minimumWidth:fractionDigits);
writeLn('-(base) ** exponent = ', -(base) pow exponent:minimumWidth:fractionDigits);
writeLn('(-base) ** exponent = ', (-base) pow exponent:minimumWidth:fractionDigits);
writeLn('-(base ** exponent) = ', -(base pow exponent):minimumWidth:fractionDigits)
end
end;
 
{ === MAIN =================================================================== }
begin
testIntegerPower(-5, 2);
testIntegerPower(+5, 2);
testIntegerPower(-5, 3);
testIntegerPower( 5, 3);
testRealPower(-5.0, 2);
testRealPower(+5.0, 2);
testRealPower(-5E0, 3);
testRealPower(+5E0, 3)
end.</lang>
{{out}}
=====> testIntegerPower <=====
base = -5
exponent = 2
-base pow exponent = -25
-(base) pow exponent = -25
(-base) pow exponent = 25
-(base pow exponent) = -25
=====> testIntegerPower <=====
base = 5
exponent = 2
-base pow exponent = -25
-(base) pow exponent = -25
(-base) pow exponent = 25
-(base pow exponent) = -25
=====> testIntegerPower <=====
base = -5
exponent = 3
-base pow exponent = 125
-(base) pow exponent = 125
(-base) pow exponent = 125
-(base pow exponent) = 125
=====> testIntegerPower <=====
base = 5
exponent = 3
-base pow exponent = -125
-(base) pow exponent = -125
(-base) pow exponent = -125
-(base pow exponent) = -125
======> testRealPower <======
base = -5.00
exponent = 2
-base ** exponent = -25.00
-(base) ** exponent = -25.00
(-base) ** exponent = 25.00
-(base ** exponent) = -25.00
======> testRealPower <======
base = 5.00
exponent = 2
-base ** exponent = -25.00
-(base) ** exponent = -25.00
(-base) ** exponent = illegal
-(base ** exponent) = -25.00
======> testRealPower <======
base = -5.00
exponent = 3
-base ** exponent = 125.00
-(base) ** exponent = 125.00
(-base) ** exponent = 125.00
-(base ** exponent) = 125.00
======> testRealPower <======
base = 5.00
exponent = 3
-base ** exponent = -125.00
-(base) ** exponent = -125.00
(-base) ** exponent = illegal
-(base ** exponent) = -125.00
Since there are ''two'' different power operators available, both accepting operands of different data types, having different limits, and yielding different data types, it was not sensible to produce a table similar to other entries on this page.
 
=={{header|Perl}}==
149

edits