How to resolve the algorithm Exponentiation operator step by step in the Seed7 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Exponentiation operator step by step in the Seed7 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 Seed7 programming language
Source code in the seed7 programming language
const func integer: intPow (in var integer: base, in var integer: exponent) is func
result
var integer: result is 0;
begin
if exponent < 0 then
raise(NUMERIC_ERROR);
else
if odd(exponent) then
result := base;
else
result := 1;
end if;
exponent := exponent div 2;
while exponent <> 0 do
base *:= base;
if odd(exponent) then
result *:= base;
end if;
exponent := exponent div 2;
end while;
end if;
end func;
const func float: fltIPow (in var float: base, in var integer: exponent) is func
result
var float: power is 1.0;
local
var integer: stop is 0;
begin
if base = 0.0 then
if exponent < 0 then
power := Infinity;
elsif exponent > 0 then
power := 0.0;
end if;
else
if exponent < 0 then
stop := -1;
end if;
if odd(exponent) then
power := base;
end if;
exponent >>:= 1;
while exponent <> stop do
base *:= base;
if odd(exponent) then
power *:= base;
end if;
exponent >>:= 1;
end while;
if stop = -1 then
power := 1.0 / power;
end if;
end if;
end func;
$ syntax expr: .(). ^* .() is <- 4;
const func integer: (in var integer: base) ^* (in var integer: exponent) is
return intPow(base, exponent);
const func float: (in var float: base) ^* (in var integer: exponent) is
return fltIPow(base, exponent);
You may also check:How to resolve the algorithm AVL tree step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Classes step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the MUMPS programming language
You may also check:How to resolve the algorithm Rosetta Code/Fix code tags step by step in the Nim programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Middle-square method step by step in the F# programming language