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

Published on 7 June 2024 03:52 AM
#C

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

The C code provided defines two different functions to calculate the power of a number. The first function, ipow(), is used to calculate the integer power of an integer base. The second function, dpow(), is used to calculate the double-precision floating-point power of a double-precision floating-point base. Additionally, the code includes a generic generic_pow() function that can be used to calculate the power of either an integer or a double-precision floating-point number based on the type of the base argument.

Here's a detailed explanation of the code:

  • ipow() Function:

    • This function takes two integer arguments: base and exp. It calculates the integer power of base raised to the power of exp.
    • It uses an iterative approach to calculate the power. It initializes a variable pow to the value of base and a variable v to 1.
    • It checks if exp is negative. If exp is negative, it asserts that base is not 0 (to avoid division by zero) and returns 0 if base*base != 1 (i.e., base is odd), and returns base if exp is odd, or 1 if exp is even.
    • It iterates until exp becomes 0. In each iteration, it multiplies v by pow if the least significant bit of exp is 1. It then multiplies pow by itself and shifts exp right by 1 bit.
    • Finally, it returns the value of v, which is the result of base raised to the power of exp.
  • dpow() Function:

    • This function takes two double-precision floating-point arguments: base and exp. It calculates the double-precision floating-point power of base raised to the power of exp.
    • It uses a similar iterative approach as the ipow() function. It initializes a variable pow to the value of base (or 1.0 if exp is negative) and a variable v to 1.0.
    • It checks if exp is negative. If exp is negative, it makes exp positive.
    • It iterates until exp becomes 0. In each iteration, it multiplies v by pow if the least significant bit of exp is 1. It then multiplies pow by itself and shifts exp right by 1 bit.
    • Finally, it returns the value of v, which is the result of base raised to the power of exp.
  • generic_pow() Macro:

    • This macro uses the C language's _Generic() feature to select the appropriate function to call based on the type of the base argument.
    • It takes two arguments: base and exp.
    • If the type of base is double, it calls the dpow() function with the given arguments.
    • If the type of base is int, it calls the ipow() function with the given arguments.
    • It returns the result of the function call.
  • Main Function:

    • The main function demonstrates the usage of the ipow(), dpow(), and generic_pow() functions.
    • It prints the result of 2^6, 2^-6, 2.71^6, and 2.71^-6 using both the ipow()/dpow() functions and the generic_pow() macro.

Source code in the c programming language

#include <stdio.h>
#include <assert.h>

int ipow(int base, int exp)
{
   int pow = base;
   int v = 1;
   if (exp < 0) {
      assert (base != 0);  /* divide by zero */
      return (base*base != 1)? 0: (exp&1)? base : 1;
   }
       
   while(exp > 0 )
   {
      if (exp & 1) v *= pow;
      pow *= pow;
      exp >>= 1;
    }
   return v;
}

double dpow(double base, int exp)
{
   double v=1.0;
   double pow = (exp <0)? 1.0/base : base;
   if (exp < 0) exp = - exp;

   while(exp > 0 )
   {
      if (exp & 1) v *= pow;
      pow *= pow;
      exp >>= 1;
   }
   return v;
}

int main()
{
    printf("2^6 = %d\n", ipow(2,6));
    printf("2^-6 = %d\n", ipow(2,-6));
    printf("2.71^6 = %lf\n", dpow(2.71,6));
    printf("2.71^-6 = %lf\n", dpow(2.71,-6));
}


#define generic_pow(base, exp)\
    _Generic((base),\
            double: dpow,\
            int: ipow)\
    (base, exp)

int main()
{
    printf("2^6 = %d\n", generic_pow(2,6));
    printf("2^-6 = %d\n", generic_pow(2,-6));
    printf("2.71^6 = %lf\n", generic_pow(2.71,6));
    printf("2.71^-6 = %lf\n", generic_pow(2.71,-6));
}


  

You may also check:How to resolve the algorithm Execute HQ9+ step by step in the DWScript programming language
You may also check:How to resolve the algorithm Perfect shuffle step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Grayscale image step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Variadic function step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the Haskell programming language