How to resolve the algorithm Exponentiation operator step by step in the FutureBasic programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Exponentiation operator step by step in the FutureBasic 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 FutureBasic programming language
Source code in the futurebasic programming language
local fn CustomPOW( base as double, exponent as NSInteger ) as double
double power = base, result
NSUInteger i
if exponent = 0.0 then result = 1.0 : exit fn
if exponent = 1.0 then result = base : exit fn
if exponent < 0.0
for i = 2 to -exponent
power = power * base
next
result = 1.0/power : exit fn
end if
for i = 2 to exponent
power = power * base
next
result = power
end fn = result
print "Custom POW function:"
print "fn CustomPOW( 2, 2 ) = "; fn CustomPOW( 2, 2 )
print "fn CustomPOW( 2.5, 2 ) = "; fn CustomPOW( 2.5, 2 )
print "fn CustomPOW( 2, -3 ) = "; fn CustomPOW( 2, -3 )
print "fn CustomPOW( 1.78, 3 ) = "; fn CustomPOW( 1.78, 3 )
print "fn CustomPOW( 5.5, 5 ) = "; fn CustomPOW( 5.5, 5 )
print "fn CustomPOW( 4.5, 2 ) = "; fn CustomPOW( 4.5, 2 )
print "fn CustomPOW( -1, -3 ) = "; fn CustomPOW( -1, -3 )
print
print "Native FB ^ operator:"
print "2^2 = "; 2^2
print "2.5^2 = "; 2.5^2
print "2^-3 = "; 2^-3
print "1.78^3 = "; 1.78^3
print "5.5^5 = "; 5.5^5
print "4.5^2 = "; 4.5^2
print "-1^=3 = "; -1^-3
print
print "Native FB fn POW function:"
print "fn POW( 2, 2 ) = "; fn POW( 2, 2 )
print "fn POW( 2.5, 2 ) = "; fn POW( 2.5, 2 )
print "fn POW( 2, -3 ) = "; fn POW( 2, -3 )
print "fn POW( 1.78, 3 ) = "; fn POW( 1.78, 3 )
print "fn POW( 5.5, 5 ) = "; fn POW( 5.5, 5 )
print "fn POW( 4.5, 2 ) = "; fn POW( 4.5, 2 )
print "fn POW( -1, -3 ) = "; fn POW( -1, -3 )
print
HandleEvents
You may also check:How to resolve the algorithm Sleep step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Four bit adder step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Topological sort step by step in the Go programming language
You may also check:How to resolve the algorithm Dutch national flag problem step by step in the Ada programming language
You may also check:How to resolve the algorithm Empty string step by step in the Axe programming language