How to resolve the algorithm Exponentiation operator step by step in the Arturo programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Exponentiation operator step by step in the Arturo programming language
Table of Contents
Problem Statement
Most programming languages have a built-in implementation of exponentiation.
Re-implement integer exponentiation for both intint and floatint as both a procedure, and an operator (if your language supports operator definition). If the language supports operator (or procedure) overloading, then an overloaded form should be provided for both intint and floatint variants.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Exponentiation operator step by step in the Arturo programming language
Source code in the arturo programming language
myPow: function [base,xp][
if xp < 0 [
(floating? base)? -> return 1 // myPow base neg xp
-> return 1 / myPow base neg xp
]
if xp = 0 ->
return 1
ans: 1
while [xp > 0][
ans: ans * base
xp: xp - 1
]
return ans
]
print ["2 ^ 3 =" myPow 2 3]
print ["1 ^ -10 =" myPow 1 neg 10]
print ["-1 ^ -3 =" myPow neg 1 neg 3]
print ""
print ["2.0 ^ -3 =" myPow 2.0 neg 3]
print ["1.5 ^ 0 =" myPow 1.5 0]
print ["4.5 ^ 2 =" myPow 4.5 2]
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the Lua programming language
You may also check:How to resolve the algorithm Roots of unity step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Primorial numbers step by step in the Clojure programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the zkl programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the mIRC Scripting Language programming language