Exponentiation with infix operators in (or operating on) the base
(Many programming languages, especially those with extended─precision integer arithmetic, usually
support one of **
, ^
, ↑
or some such for exponentiation.)
Some languages treat/honor infix operators when performing exponentiation (raising
numbers to some power by the language's exponentiation operator, if the computer
programming language has one).
Other programming languages may make use of the POW or some other BIF
(Built─In Ffunction), or some other library service.
If your language's exponentiation operator is not one of the usual ones, please comment on how to recognize it.
This task will deal with the case where there is some form of an infix operator operating
in (or operating on) the base.
- Example
A negative five raised to the 3rd power could be specified as:
-5 ** 3 or as -(5) ** 3 or as (-5) ** 3 or as something else
(Not all computer programming languages have an exponential operator and/or support these syntax expression(s).
- Task
-
- compute and display exponentiation with a possible infix operator, whether specified and/or implied/inferred.
- Raise the following numbers (integer or real):
- -5 and
- +5
- to the following powers:
- 2nd and
- 3rd
- using the following expressions (if applicable in your language):
- -x**p
- -(x)**p
- (-x)**p
- -(x**p)
- Show here (on this page) the four (or more) types of symbolic expressions for each number and power.
Try to present the results in the same format/manner as the other programming entries to make any differences apparent.
The variables may be of any type(s) that is/are applicable in your language.
- Related tasks
Factor
<lang factor>USING: infix locals prettyprint sequences sequences.generalizations sequences.repeating ;
- row ( x p -- seq )
x p "-x**p" [infix -x**p infix] "-(x)**p" [infix -(x)**p infix] "(-x)**p" [infix (-x)**p infix] "-(x**p)" [infix -(x**p) infix] 10 narray ;
{ "x value" "p value" } { "expression" "result" } 8 cycle append -5 2 row -5 3 row 5 2 row 5 3 row 5 narray simple-table.</lang>
- Output:
x value p value expression result expression result expression result expression result -5 2 -x**p 25 -(x)**p 25 (-x)**p 25 -(x**p) -25 -5 3 -x**p 125 -(x)**p 125 (-x)**p 125 -(x**p) 125 5 2 -x**p 25 -(x)**p 25 (-x)**p 25 -(x**p) -25 5 3 -x**p -125 -(x)**p -125 (-x)**p -125 -(x**p) -125
Raku
In Raku by default, infix exponentiation binds tighter than unary negation.
<lang perl6>sub infix-exp (\x, \p) {
printf "x = %2d p = %2d │ %s = %4d │ %s = %4d │ %s = %4d │ %s = %4d\n", x, p, '-x**p', -x**p, '-(x)**p', -(x)**p, '(-x)**p', (-x)**p, '-(x**p)', -(x**p);
}
say 'Default precedence: infix exponentiation is tighter (higher) precedence than unary negation.'; -> $x, $p { infix-exp($x, $p) } for -5, 2, -5, 3, 5, 2, 5, 3;
- It is trivial in Raku to define your own infix operators with
- whatever precedence level meets the needs of your program.
sub infix:<↑> is looser(&infix:<->) { $^a ** $^b }
sub infix-loose-exp (\x, \p) {
printf "x = %2d p = %2d │ %s = %4d │ %s = %4d │ %s = %4d │ %s = %4d\n", x, p, '-x↑p ', -x↑p, '-(x)↑p ', -(x)↑p, '(-x)↑p ', (-x)↑p, '-(x↑p) ', -(x↑p);
}
say "\nEasily modified: custom loose infix exponentiation is looser (lower) precedence than unary negation."; -> $x, $p { infix-loose-exp($x, $p) } for -5, 2, -5, 3, 5, 2, 5, 3;</lang>
- Output:
Default precedence: infix exponentiation is tighter (higher) precedence than unary negation. 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 Easily modified: custom loose infix exponentiation is looser (lower) precedence than unary negation. 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
REXX
<lang rexx>/*REXX program shows exponentition with an infix operator (implied and/or specified).*/ _= '─'; ! = '║'; mJunct= '─╫─'; bJunct= '─╨─' /*define some special glyphs. */
say @(' x ', 5) @(" p ", 5) ! say @('value', 5) @("value", 5) copies(! @('expression',10) @("result",6)" ", 4) say @( , 5, _) @("", 5, _)copies(mJunct || @(, 10, _) @("", 6, _) , 4)
do x=-5 to 5 by 10 /*assign -5 and 5 to X. */ do p= 2 to 3 /*assign 2 and 3 to P. */
a = -x**p ; b = -(x)**p ; c = (-x)**p ; d = -(x**p) ae= '-x**p'; be= "-(x)**p"; ce= '(-x)**p'; de= "-(x**p)" say @(x,5) @(p,5) ! @(ae, 10) right(a, 5)" " , ! @(be, 10) right(b, 5)" " , ! @(ce, 10) right(c, 5)" " , ! @(de, 10) right(d, 5) end /*p*/ end /*x*/
say @( , 5, _) @(, 5, _)copies(bJunct || @(, 10, _) @(, 6, _) , 4) exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ @: parse arg txt, w, fill; if fill== then fill= ' '; return center( txt, w, fill) </lang>
- output when using the internal default input:
x p ║ value value ║ expression result ║ expression result ║ expression result ║ expression result ───── ──────╫─────────── ───────╫─────────── ───────╫─────────── ───────╫─────────── ────── -5 2 ║ -x**p 25 ║ -(x)**p 25 ║ (-x)**p 25 ║ -(x**p) -25 -5 3 ║ -x**p 125 ║ -(x)**p 125 ║ (-x)**p 125 ║ -(x**p) 125 5 2 ║ -x**p 25 ║ -(x)**p 25 ║ (-x)**p 25 ║ -(x**p) -25 5 3 ║ -x**p -125 ║ -(x)**p -125 ║ (-x)**p -125 ║ -(x**p) -125 ───── ──────╨─────────── ───────╨─────────── ───────╨─────────── ───────╨─────────── ──────