How to resolve the algorithm Exponentiation operator step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Exponentiation operator step by step in the FreeBASIC 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 FreeBASIC programming language

Source code in the freebasic programming language

' FB 1.05.0

' Note that 'base' is a keyword in FB, so we use 'base_' instead as a parameter

Function Pow Overload (base_ As Double, exponent As Integer) As Double
  If exponent = 0.0 Then Return 1.0
  If exponent = 1.0 Then Return base_
  If exponent < 0.0 Then Return 1.0 / Pow(base_, -exponent)
  Dim power As Double = base_
  For i As Integer = 2 To exponent
     power *= base_
  Next
  Return power
End Function

Function Pow Overload(base_ As Integer, exponent As Integer) As Double
   Return Pow(CDbl(base_), exponent)
End Function

' check results of these functions using FB's built in '^' operator
Print "Pow(2, 2)       = "; Pow(2, 2)
Print "Pow(2.5, 2)     = "; Pow(2.5, 2)
Print "Pow(2, -3)      = "; Pow(2, -3) 
Print "Pow(1.78, 3)    = "; Pow(1.78, 3)
Print
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
Print "Press any key to quit"
Sleep

  

You may also check:How to resolve the algorithm Retrieve and search chat history step by step in the Python programming language
You may also check:How to resolve the algorithm Sudoku step by step in the jq programming language
You may also check:How to resolve the algorithm Elementary cellular automaton step by step in the MiniScript programming language
You may also check:How to resolve the algorithm String case step by step in the Groovy programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the MUMPS programming language