简体   繁体   中英

How to set precision for complex numbers in C++

I have the following code where I need to add two complex numbers. Formula is working, however, I am not able to get rid of the scientific notation. I'm new to C++, and not sure how to use setPrecision.

My code in .hpp:

    class ComplexNumber
    {
    public:

//--constructors
    ComplexNumber();
    ComplexNumber(double r, double i);
    ComplexNumber(const ComplexNumber &cn);

    void print();
    ComplexNumber add(const ComplexNumber &rhs);
    private:
        double re, im;
    };

My code in .cpp file

//--constructors
ComplexNumber::ComplexNumber()
{

}
ComplexNumber::ComplexNumber(double r, double i)
{

}
ComplexNumber::ComplexNumber(const ComplexNumber &cn)
{

}    

void ComplexNumber::print()
    {
        std::cout << this->re << " + " << this->im << "i" << std::endl;
    }

    ComplexNumber ComplexNumber::add(const ComplexNumber &rhs)
    {
        return ComplexNumber(this-> re + rhs.re, this->im + rhs.im);
    }

My main:

int main(int argc, const char * argv[]) {
    ComplexNumber a(1,2);
    ComplexNumber b(3,4);
    ComplexNumber c;

    c = a.add(b);
    c.print();
    return 0;
}

Output: 4.94066e-324 + 6.95322e-310i

it should be 4 + 6i

None of your constructors actually do anything. That means the member variables will be uninitialized, their values will be indeterminate and using them will lead to undefined behavior

You need to actually initialize the members in the constructors.

Works as expected after fixing the syntax (making it an aggregate):

#include <iostream>

struct ComplexNumber
{
    void print();
    ComplexNumber add(const ComplexNumber &rhs);
    double re, im;
};

void ComplexNumber::print()
{
    std::cout << this->re << " + " << this->im << "i" << std::endl;
}

ComplexNumber ComplexNumber::add(const ComplexNumber &rhs)
{
    return ComplexNumber{this-> re + rhs.re, this->im + rhs.im};
}

int main(int argc, const char * argv[]) {
    ComplexNumber a{1,2};
    ComplexNumber b{3,4};
    ComplexNumber c;

    c = a.add(b);
    c.print();
    return 0;
}

Precision is not the issue here.

Your two-argument constructor needs to be implemented as follows

ComplexNumber::ComplexNumber(double r, double i) : re(r), im(i)
{
}

otherwise the class members stay uninitialised .

Reading uninitialised variables is undefined behaviour in C++; your output is a manifestation of that.

It's stylistically acceptable (and in my opinion preferable for a number class) to write

ComplexNumber() = default;

for your default constructor; if you want it to keep the class members uninitialised, as is the case for the C++ built-in types. Just don't read the member variables before assigning them to something.

Finally, rely on the compiler to generate the copy constructor for you: there's no need to build your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM