Exponentiation with infix operators in (or operating on) the base: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|Lua}}: added Lua solution) |
||
Line 303: | Line 303: | ||
x is 5, p is 3, -x^p is-125, -(x)^p is -125, (-x)^p is -125, -(x^p) is -125 |
x is 5, p is 3, -x^p is-125, -(x)^p is -125, (-x)^p is -125, -(x^p) is -125 |
||
</pre> |
</pre> |
||
=={{header|Lua}}== |
|||
Lua < 5.3 has a single double-precision numeric type. Lua >= 5.3 adds an integer numeric type. "^" is supported as an infix exponentiation operator for both types. |
|||
<lang lua>mathtype = math.type or type -- polyfill <5.3 |
|||
function test(xs, ps) |
|||
for _,x in ipairs(xs) do |
|||
for _,p in ipairs(ps) do |
|||
print(string.format("%2.f %7s %2.f %7s %4.f %4.f %4.f %4.f", x, mathtype(x), p, mathtype(p), -x^p, -(x)^p, (-x)^p, -(x^p))) |
|||
end |
|||
end |
|||
end |
|||
print(" x type(x) p type(p) -x^p -(x)^p (-x)^p -(x^p)") |
|||
print("-- ------- -- ------- ------ ------ ------ ------") |
|||
test( {-5.,5.}, {2.,3.} ) -- "float" (or "number" if <5.3) |
|||
if math.type then -- if >=5.3 |
|||
test( {-5,5}, {2,3} ) -- "integer" |
|||
end</lang> |
|||
{{out}} |
|||
<pre> x type(x) p type(p) -x^p -(x)^p (-x)^p -(x^p) |
|||
-- ------- -- ------- ------ ------ ------ ------ |
|||
-5 float 2 float -25 -25 25 -25 |
|||
-5 float 3 float 125 125 125 125 |
|||
5 float 2 float -25 -25 25 -25 |
|||
5 float 3 float -125 -125 -125 -125 |
|||
-5 integer 2 integer -25 -25 25 -25 |
|||
-5 integer 3 integer 125 125 125 125 |
|||
5 integer 2 integer -25 -25 25 -25 |
|||
5 integer 3 integer -125 -125 -125 -125</pre> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |