How to resolve the algorithm Exponentiation operator step by step in the C programming language
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
andexp
. It calculates the integer power ofbase
raised to the power ofexp
. - It uses an iterative approach to calculate the power. It initializes a variable
pow
to the value ofbase
and a variablev
to 1. - It checks if
exp
is negative. Ifexp
is negative, it asserts thatbase
is not 0 (to avoid division by zero) and returns 0 ifbase*base != 1
(i.e.,base
is odd), and returnsbase
ifexp
is odd, or 1 ifexp
is even. - It iterates until
exp
becomes 0. In each iteration, it multipliesv
bypow
if the least significant bit ofexp
is 1. It then multipliespow
by itself and shiftsexp
right by 1 bit. - Finally, it returns the value of
v
, which is the result ofbase
raised to the power ofexp
.
- This function takes two integer arguments:
-
dpow() Function:
- This function takes two double-precision floating-point arguments:
base
andexp
. It calculates the double-precision floating-point power ofbase
raised to the power ofexp
. - It uses a similar iterative approach as the
ipow()
function. It initializes a variablepow
to the value ofbase
(or 1.0 ifexp
is negative) and a variablev
to 1.0. - It checks if
exp
is negative. Ifexp
is negative, it makesexp
positive. - It iterates until
exp
becomes 0. In each iteration, it multipliesv
bypow
if the least significant bit ofexp
is 1. It then multipliespow
by itself and shiftsexp
right by 1 bit. - Finally, it returns the value of
v
, which is the result ofbase
raised to the power ofexp
.
- This function takes two double-precision floating-point arguments:
-
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
andexp
. - If the type of
base
isdouble
, it calls thedpow()
function with the given arguments. - If the type of
base
isint
, it calls theipow()
function with the given arguments. - It returns the result of the function call.
- This macro uses the C language's _Generic() feature to select the appropriate function to call based on the type of the
-
Main Function:
- The main function demonstrates the usage of the
ipow()
,dpow()
, andgeneric_pow()
functions. - It prints the result of
2^6
,2^-6
,2.71^6
, and2.71^-6
using both theipow()
/dpow()
functions and thegeneric_pow()
macro.
- The main function demonstrates the usage of the
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