Model for exponent function

Mar 23, 2019 at 4:54am
Having difficulties getting values for negative exponents. Keep getting 0. Any help would be appreciated.* side note: I know I can just use pow function w/ cmath but practicing use of for loops.*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

using namespace std;

int exponentFcn (int baseNum, int powNum) {
    double result;
    int i;
    if (powNum >= 0){
        result = 1;
        for (int i = 0; i < powNum; i++){
        result = result * baseNum;
    }
    return result;
    }
    else if (powNum < 0){
        result = 1;
        for (int i = 0; i > powNum; i--){
        result = result * baseNum;
    }
    return 1 / result;
    }
    else {
        cout << "Invalid entry";
    }

}
int main()

{
    int baseNum;
    int powNum;
    cout << "Enter base number: ";
    cin >> baseNum;
    cout << "Enter power: ";
    cin >> powNum;
    cout << "Answer: " << exponentFcn(baseNum,powNum);
    return 0;
 }
Mar 23, 2019 at 5:23am
> return 1 / result;
Any negative power will always be zero, in integer arithmetic.

> int exponentFcn (int baseNum, int powNum)
Try returning a double, like what the type of result is already.
Mar 23, 2019 at 5:25am
What exactly do you think is happening here?
 
for (int i = 0; i > powNum; i--)

And your else clause will never happen. You should get rid of it and the else if (...) should just be an else.
Mar 23, 2019 at 5:58am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
#include <iomanip>

constexpr double ipow( int base, int exp )
{
    if( exp == 0 ) return 1 ; // note: ipow(0,0) == 1 (this mimics std::pow)
    else if( exp<0 ) return 1.0 / ipow( base, -exp ) ;
    else return base * ipow( base, exp-1 ) ;
}

int main()
{
    std::cout << std::fixed << std::showpos ;

    for( int base : { -2, -1, 0, 1, 2 } )
        for( int exp : { -5, -2, -1, 0, 1, 2, 5 } )
        {
            std::cout << base << " ^ " << exp
                      << " == " << std::setw(10) << ipow( base, exp )
                      << " == " << std::setw(10) << std::pow( base, exp ) << '\n' ;
        }
}

http://coliru.stacked-crooked.com/a/101370b37011b253
Mar 23, 2019 at 12:42pm
@salem c thanks. I just had to change the function and its parameters to doubles.
Mar 23, 2019 at 7:25pm
another way... using an unrolled loop, any input type will work long as the exponent itself is only an int (double type exponents are tricky).

http://www.cplusplus.com/forum/beginner/250227/

if you want to support negative exponent, would need to 1/x the result at the end AND avoid tapping the sign bit as part of the power, similar to what you did but watch that sign bit in this approach.
Last edited on Mar 23, 2019 at 7:28pm
Mar 24, 2019 at 9:59am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

double ipow( int base, int exponent )
{
   if ( exponent < 0 ) return 1.0 / ipow( base, -exponent );

   double result = 1.0;
   for ( ; exponent; base *= base, exponent /= 2 ) if ( exponent % 2 ) result *= base;
   return result;
}


int main()
{
   #define FORMAT << fixed << showpos << setw( 12 ) <<

   for ( int base : { -5, -2, -1, 0, 1, 2, 5 } )
   {
      for ( int exponent : { -5, -2, -1, 0, 1, 2, 5 } )
      {
         cout << base << " ^ " << exponent 
              << " = " FORMAT ipow( base, exponent )
              << " = " FORMAT pow( base, exponent ) << '\n';
      }
   }
}
Topic archived. No new replies allowed.