How to resolve the algorithm Exponentiation with infix operators in (or operating on) the base step by step in the Wren programming language
How to resolve the algorithm Exponentiation with infix operators in (or operating on) the base step by step in the Wren programming language
Table of Contents
Problem Statement
(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.
A negative five raised to the 3rd power could be specified as:
(Not all computer programming languages have an exponential operator and/or support these syntax expression(s).
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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Exponentiation with infix operators in (or operating on) the base step by step in the Wren programming language
Source code in the wren programming language
import "/fmt" for Fmt
class Num2 {
construct new(n) { _n = n }
n { _n}
^(exp) {
if (exp is Num2) exp = exp.n
return Num2.new(_n.pow(exp))
}
- { Num2.new(-_n) }
toString { _n.toString }
}
var ops = ["-x^p", "-(x)^p", "(-x)^p", "-(x^p)"]
for (x in [Num2.new(-5), Num2.new(5)]) {
for (p in [Num2.new(2), Num2.new(3)]) {
Fmt.write("x = $2s p = $s | ", x, p)
Fmt.write("$s = $4s | ", ops[0], -x^p)
Fmt.write("$s = $4s | ", ops[1], -(x)^p)
Fmt.write("$s = $4s | ", ops[2], (-x)^p)
Fmt.print("$s = $4s", ops[3], -(x^p))
}
}
You may also check:How to resolve the algorithm Count in octal step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Draw a pixel step by step in the Perl programming language
You may also check:How to resolve the algorithm Multiplication tables step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Magic squares of doubly even order step by step in the jq programming language