Smokey Van Den Berg
Smokey Van Den Berg

Reputation: 171

Dealing with negative exponents without using POW in C++

Hey guys i'm working through a book and i'm little baffled with the sub objective of the following question..

Powers of numbers can be calculated by multiplying the number by itself for as many times as the value of the exponent. For example, 2 raised to the power 4 can be calculated by multiplying 2 by itself 4 times to get 16. Write a program that: 1. inputs a double as the base number and an int as the exponent;

double base, ans = 0;
int exponent;

cout << "This program will calculate the power of any number that you enter.." << endl;
cout << "Please enter your base number" << endl;
cin >> base;
cout << "Thank you" << endl;
cout << "Please enter the Exponent" << endl;
cin >> exponent;
  1. multiplies the base number by itself using a loop that repeats for the number of times in the int;

    while (exponent != 0)
    {
        ans = ans + (base * base);
        --exponent;
    }
    
  2. outputs the exponential value calculated.

    cout << "The answer is " << ans << endl;
    

Use an if statement for the special case where the output is 1 if the int value is 0.

if (exponent == 0)
{
    cout << "The answer is " << 1 << " because the exponent value entered was 0" << endl;
}

For a more challenging version, deal with the case where the exponent is negative. (this is where my problem sits.. i cant get the working without using POW) here is what i tried:

else if (exponent < 0)
{
    ans = pow(base, exponent);
    /*
    ans++;
    while (exponent != 0)
    {
        ans = 1/(ans + (base * base));
        ++exponent;
    }
    */
    cout << "The answer is " << ans << endl;
}

I did some searching online and found this: "To represent a negative exponent. All you have to do is divide 1 over the positive exponent. Ex: 6^-2 = 1/(6^2)."

and this: "Well that's because in C++ the ^ is the bitwise xor operator...you will need to use the pow() in order to do it."

any help or comments are welcome thanks in advance

Upvotes: 0

Views: 11535

Answers (2)

zaahidManzoor
zaahidManzoor

Reputation: 1

#include <iostream>
using namespace std;

double raise_to_power(double, double);

main(){
double x;
double y;

cout<<"plz enter the number";
cin>>x;
cout<<"plz enter the power";
cin>>y;

cout<<"The power of a number is "<<raise_to_power(x,y);

    
}

double raise_to_power(double number, double power){
double i;
double result;
result=1.0;

for(i=-1; i>=power && power!=0; i-- ){
    
    result=result*(1/number);
    
 }
 
 
 return result;
}

Upvotes: -1

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 123431

It is just a matter of conventions and definition that in mathematics (ie ^ is exponentation here)

a ^ (-b) = 1 / (a^b)

Hence if you know how to compute (a^b) you just need to take the reciprocal to get the desired result. Something along the line of:

double my_pow(double a,double b) {
      if (b < 0) return 1.0 / pow(a,std::abs(b));
      else return pow(a,b);
}

As I understood you, you want to write your own implementation of pow, so this is just to illustrate how to use a pow that works for positive exponents also with negative ones.

Concerning your code, I dont see how this

while (exponent != 0)
{
    ans = ans + (base * base);
    --exponent;
}

can be correct.

multiplies the base number by itself using a loop that repeats for the number of times in the int;

No. Thats not really what that loop is doing.

I'd rather expect to see something like this

ans = 1;
for (int i=0; i< exponent;++i) {
    ans *= base;
}

PS: If you do actually not want to write your own, you can use std::pow. It can be used with negative exponents.

Upvotes: 2

Related Questions