1

I've written a naive (only accepts integer exponents) power function for complex numbers (a home made class) using a simple for loop that multiplies the result for the original number n times:

C pow(C c, int e) {
    C res = 1;
    for (int i = 0; i==abs(e); ++i) res=res*c;
    return e > 0 ? res : static_cast<C>(1/res);
}

When I try to execute this, e.g.

C c(1,2);
cout << pow(c,3) << endl;

I always get 1, because the for loop doesn't execute (I checked). Here's the full code:

#include <cmath>
#include <stdexcept>
#include <iostream>
using namespace std;
struct C {
    // a + bi in C forall a, b in R
    double a;
    double b;
    C() = default;
    C(double f, double i=0): a(f), b(i) {}
    C operator+(C c) {return C(a+c.a,b+c.b);}
    C operator-(C c) {return C(a-c.a,b-c.b);}
    C operator*(C c) {return C(a*c.a-b*c.b,a*c.b+c.a*b);}
    C operator/(C c) {return C((a*c.a+b*c.b)/(pow(c.a,2)+pow(c.b,2)),(b*c.a - a*c.b)/(pow(c.a,2)+pow(c.b,2)));}
    operator double(){ if(b == 0) 
                        return double(a);
                       else 
                        throw invalid_argument(
                        "can't convert a complex number with an imaginary part to a double");}
};
C pow(C c, int e) {
    C res = 1;
    for (int i = 0; i==abs(e); ++i) {
        res=res*c;
        // check wether the loop executes
        cout << res << endl;}
    return e > 0 ? res : static_cast<C>(1/res);
}

ostream &operator<<(ostream &o, C c) { return c.b ? cout << c.a << " + " << c.b << "i " : cout << c.a;}

int main() {
C c(1,2), d(-1,3), a;
        cout << c << "^3 = " << pow(c,3) << endl;}
5
  • This loop will run as long as i and e is the same value. Was that the intent? Commented Apr 20, 2015 at 10:14
  • 4
    Probably i==abs(e); should be i<=abs(e); Commented Apr 20, 2015 at 10:14
  • @RichardCritten Shouldn't it be strictly less i<abs(e) Commented Apr 20, 2015 at 10:15
  • @halex wasn't sure that's why the "Probably". Didn't analyse the code assumed a typo. Commented Apr 20, 2015 at 10:17
  • Look out for pow(0, -2), what will (1/res) do ? Commented Apr 20, 2015 at 10:33

2 Answers 2

1

What you wrote will read as follows:

for (int i = 0; i == abs(e); ++i) 

initialize i with 0 and while i is equal to the absolute value of e (i.e. 3 at the beginning of the function call), do something

It should rather be

for (int i = 0; i < abs(e); ++i) 

Tip: the code will throw at the first iteration due to the double conversion operator (and caused by a*c.b + c.a*b), but this is another issue: fix your complex (i.e. with imaginary part) printing function or implement a pretty printing method or such.

Sign up to request clarification or add additional context in comments.

3 Comments

What an obvius error it was! sorry for the bother and thanks for the extra tips
You are right that after fixing the stop condition it throws an exception, although I don't see how a*c.b + c.a*b may be causing that; isn't it just a double moltiplication that gets converted in a C right afterwards ?
It is not the multiplication, the res variable is being converted to a double and (tried to be) printed.
0

you should be using i != abs(e) or i < abs(e) as for loop condition. Currently you are using i == abs(e) which will fail in first try because:

i = 0 abs(e) = 3

so 0 == 3 is false and hence for loop will not execute.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.