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

Published on 12 May 2024 09:40 PM
#Go

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

This code defines two functions expI and expF, which calculate the exponentiation of two numbers. The expI function performs integer exponentiation, while the expF function performs floating-point exponentiation.

Main Function

The main function serves as the entry point of the program:

  • It defines a nested function ti that takes two integer arguments b (base) and p (power) and prints the result of b^p using the expI function.
  • It tests various integer exponentiation scenarios using the ti function and prints the results.
  • Similarly, the program defines a nested function tf for floating-point exponentiation using the expF function.
  • It tests various scenarios and prints the results.

expI Function (Integer Exponentiation)

  • The expI function takes two integers b and p as input.
  • It initializes a variable r to 1, which will store the result.
  • It checks if the power p is negative, in which case it returns an error.
  • It enters a loop that runs from i = 1 to p.
    • In each iteration, it multiplies the current value of r by b.
  • Finally, it returns the result r.

expF Function (Floating-Point Exponentiation)

  • The expF function takes two arguments: b of type float32 (base) and p of type int (power).
  • It uses a more efficient algorithm known as "exponentiation by squaring" or "repeated squaring":
    • It checks if the power p is negative, in which case it inverts the result later.
    • It initializes a variable r to 1, which will store the result.
    • It enters a loop that continues until p becomes 0.
      • Inside the loop, it sets a variable pow to b.
      • It calculates pow *= pow, effectively squaring pow.
      • If p is odd (i.e., p&1 == 1), it multiplies r by pow.
      • It shifts p right by 1 bit, effectively dividing it by 2.
    • If the power p was initially negative, it inverts the result r.
  • Finally, it returns the result r.

Sample Output

expI tests
2^10: 1024
2^-10: error: negative power not allowed
-2^10: -1024
-2^11: error: negative power not allowed
11^0: 1
overflow undetected
10^10: error: negative power not allowed

expF tests:
2^10: 1024
2^-10: 9.3132255e-31
-2^10: -1024
-2^11: -2048
11^0: 1
disallowed in expI, allowed here
0^-1: error: negative power not allowed

other interesting cases for 32 bit float type
10^39: 1e+39
10^-39: 1e-39
-10^39: -1e+39

Source code in the go programming language

package main

import (
    "errors"
    "fmt"
)

func expI(b, p int) (int, error) {
    if p < 0 {
        return 0, errors.New("negative power not allowed")
    }
    r := 1
    for i := 1; i <= p; i++ {
        r *= b
    }
    return r, nil
}

func expF(b float32, p int) float32 {
    var neg bool
    if p < 0 {
        neg = true
        p = -p
    }
    r := float32(1)
    for pow := b; p > 0; pow *= pow {
        if p&1 == 1 {
            r *= pow
        }
        p >>= 1
    }
    if neg {
        r = 1 / r
    }
    return r
}

func main() {
    ti := func(b, p int) {
        fmt.Printf("%d^%d: ", b, p)
        e, err := expI(b, p)
        if err != nil {
            fmt.Println(err)
        } else {
            fmt.Println(e)
        }
    }

    fmt.Println("expI tests")
    ti(2, 10)
    ti(2, -10)
    ti(-2, 10)
    ti(-2, 11)
    ti(11, 0)

    fmt.Println("overflow undetected")
    ti(10, 10)

    tf := func(b float32, p int) {
        fmt.Printf("%g^%d: %g\n", b, p, expF(b, p))
    }

    fmt.Println("\nexpF tests:")
    tf(2, 10)
    tf(2, -10)
    tf(-2, 10)
    tf(-2, 11)
    tf(11, 0)

    fmt.Println("disallowed in expI, allowed here")
    tf(0, -1)

    fmt.Println("other interesting cases for 32 bit float type")
    tf(10, 39)
    tf(10, -39)
    tf(-10, 39)
}


  

You may also check:How to resolve the algorithm Plot coordinate pairs step by step in the Stata programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the Elixir programming language
You may also check:How to resolve the algorithm XML/DOM serialization step by step in the Nim programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the ALGOL-M programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the Ruby programming language