How to set precision for complex numbers in C++ - 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.

Related

complex number wrong output

I write this code with a class named Complex with the following methods:
a. Complex() // Default constructor returning 0
b. Complex(float im, float real)
c. float getImaginary() const
d. float getReal() const
e. Complex add(const Complex& rhs) const
f. Complex subtract(const Complex& rhs) const
g. Complex multiply(const Complex& rhs) const
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imaginary;
public:
Complex();
Complex(float real=0, float im=0);
float GetReal() const;
float GetImaginary() const;
Complex Add(const Complex &rhs) const;
Complex Subtract(const Complex &rhs) const;
Complex Multiply(const Complex &rhs) const;
void SetReal(float r);
void SetImaginary(float i);
};
Complex::Complex()
{
real=imaginary=0;
}
Complex::Complex(float realpart, float imaginarypart)
{
SetReal(real);
SetImaginary(imaginarypart);
}
void Complex::SetReal(float r)
{
real = r;
}
void Complex::SetImaginary(float i)
{
imaginary = i;
}
float Complex::GetReal() const
{
return real;
}
float Complex::GetImaginary() const {
return imaginary;
}
Complex Complex::Add(const Complex &rhs) const {
return Complex(GetReal() + rhs.GetReal(), GetImaginary() + rhs.GetImaginary());
}
Complex operator+(const Complex &rhs1, const Complex &rhs2) {
return rhs1.Add(rhs2);
}
Complex Complex::Subtract(const Complex &rhs) const
{
return Complex(GetReal() - rhs.GetReal(), GetImaginary() - rhs.GetImaginary());
}
Complex operator-(const Complex &rhs1, const Complex &rhs2)
{
return rhs1.Subtract(rhs2);
}
Complex Complex::Multiply(const Complex &rhs) const
{
return Complex(GetReal() * rhs.GetReal(), GetImaginary() * rhs.GetImaginary());
}
Complex operator*(const Complex &rhs1, const Complex &rhs2)
{
return rhs1.Multiply(rhs2);
}
int main()
{
Complex c();
Complex x(-2,4);
Complex y(5,6);
Complex z = x + y;
cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
Complex e = x - y;
cout << e.GetReal() << '+' << e.GetImaginary() << 'i' << endl;
Complex r = x * y;
cout << r.GetReal() << '*' << r.GetImaginary() << 'i' << endl;
return 0;
}
but i take these output
8.99944e-039+10i
8.99944e-039+-2i
8.99944e-039*24i
Can somebody help me for this error ?
3 issues in your code:
Complex::Complex(float realpart, float imaginarypart)
{
SetReal(realpart);
SetImaginary(imaginarypart);
}
causes the warning:
<source>:24:28: warning: unused parameter 'realpart' [-Wunused-parameter]
24 | Complex::Complex(float realpart, float imaginarypart)
|
Fix it by passing realpart to SetReal or rather use the member initializer list:
Complex::Complex(float realpart, float imaginarypart) : real(realpart),imaginary(imaginarypart)
{}
Next, this:
Complex c();
is the most vexing parse. It declares a function named c taking no parameters and retuning a Complex. It is not declaring a variable c of type Complex. Write
Complex c;
Complex c{};
to call the default constructor. However, you class has two default constructors:
Complex();
Complex(float real=0, float im=0);
A default constructor is one that can be called without parameters and thats the case for both of these. Hence you currently cannot default construct a Complex, the constructors are ambigoous. I read your assignemnt as requiring you to provide two constructors:
Complex();
Complex(float real, float im); // no defaults !!
Then you will be able to default construct a Complex via Complex c; or Complex c{}; (but not via Complex c(); because thats a function declaration).
After fixing those, I get almost reasonable looking output: https://godbolt.org/z/x4xG8xzTf
Finally, your formula for multipliying two complex numbers is wrong. I'll leave that for you to fix it.

Logical error with overloaded operator C++

I've been making a class called Complex for storing and working with complex numbers. I'm having issues where my overloaded + and * functions return an obscenely large number, which I can only presume has something to do with pointers, but I'm very new to using them. I've copied the output here:
c1: 1 - i5
c2: -4 + i3
c1 + c2: -9.25596e+061 - i9.255
c1 * c2: -9.25596e+061 - i9.255
Press any key to continue . . .
Here is the class architecture:
// Complex.h
#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
friend std::ostream &operator<<(std::ostream &, const Complex &);
friend std::istream &operator>>(std::istream &, Complex &);
friend Complex &operator+(const Complex &, const Complex &);
friend Complex &operator+(const Complex &, const double &);
friend Complex &operator+(const double &, const Complex &);
friend Complex &operator*(const Complex &, const Complex &);
friend Complex &operator*(const Complex &, const double &);
friend Complex &operator*(const double &, const Complex &);
public:
explicit Complex(double re=0, double im=0);
void setReal(double re);
void setImag(double im);
double &getReal();
double &getImag();
~Complex();
private:
double real;
double imag;
};
And here I have the relevant portions of my program:
// Complex.cpp
#include "Complex.h"
ostream &operator<<(ostream &output, const Complex &num)
{
output << num.real << (num.imag >= 0 ? " + i" : " - i") << abs(num.imag);
return output;
}
Complex &operator+(const Complex &num1, const Complex &num2)
{
Complex sum(num1.real + num2.real, num1.imag + num2.imag); // Create new complex by summing elements
return sum;
}
Complex &operator*(const Complex &num1, const Complex &num2)
{
Complex prod(num1.real * num2.real - num1.imag * num2.imag, num1.real * num2.imag + num1.imag * num2.real); // Create new complex by expansion
return prod;
}
Complex::Complex(double re, double im)
{
real = re;
imag = im;
}
int main() {
Complex c1(1.0, -5.0);
Complex c2(-4.0, 3.0);
cout << "c1: " << c1 << endl;
cout << "c2: " << c2 << endl;
cout << "c1 + c2: " << c1 + c2 << endl;
cout << "c1 * c2: " << c1 * c2 << endl;
system("pause");
}
I feel bad bothering you guys about this. Do you possibly know of a site that does a good job of explaining concepts for C++ like pointers, least-privilege, polymorphism, inheritance, etc?
Sorry for asking basic questions on here, but it's hard to find resources that explain this stuff clearly.
Wrong return type:
Complex &operator+(const Complex &num1, const Complex &num2)
^^^^^^^^^
You're creating a temporary Complex and returning it by reference, and then it gets destroyed so you have a dangling reference. Since operator+ is constructing a new object and returning it, it should return it by value... so the signature should be:
Complex operator+(Complex const&, Complex const& );
The problem is that most of your overloaded operators return a reference to a local var, which is destroyed when the operator function returns, leaving you with a dangling reference. Try changing your operators to return a Complex rather than a Complex &:
Complex operator+(const Complex &num1, const Complex &num2)
{
Complex sum(num1.real + num2.real, num1.imag + num2.imag); // Create new complex by summing elements
return sum;
}

operator= in c++ (11) working direction

I'm doing a Little rational class for my Project and I overload all aritmethic operators. Well, when I try to overload operator= I have a Little and now I don't know if is my problem (i don't know how it Works) or problem of my wroten code (i wrote it bad) here's the code:
class rational{
public:
double& operator=(double& d){
d= this->num/this->den;
return d;
}
double& operator=(rational& r){
double d= r.num/r.den;
return d;
}
double& operator=(){
double d= this->num/this->den;
return d;
}
}
Ok, what's wrong? what's right? (i think that all is wrong haha)
My goal is do that:
int main(){
rational r(4, 5);
double d= r;
}
Can I do it? if yes, how?
You don't want an assignment operator for this purpose - you should instead overload a conversion operator; e.g.
class rational {
private:
int num;
int den;
public:
// ...
operator double() { return double(num) / double(den); }
};
This will allow
rational r(4, 5);
double d = double(r); // d = 0.8
The assignment operators should be used for changing the state of an existing object, if that's something you want to allow. You probably would not want to allow assignment of a double to a rational there is no unambiguous meaning for such an operation. However, you might want to provide helpers for assigning an int, say, in addition to the usual one for assigning another rational:
rational &operator=(const rational &rhs)
{
num = rhs.num;
den = rhs.den;
return *this;
}
rational &operator=(int rhs)
{
num = rhs;
den = 1;
return *this;
}
Here I think a user-defined conversion operator would be more appropriate.
class rational {
public:
rational( int iNum, int iDen ) : num( iNum ), den( iDen ) {}
// ...
operator double() { return (double)num / (double)den; }
private:
int num;
int den;
};
int main()
{
rational r( 1, 2 );
double n = r;
std::cout << r << std::endl; // output 0.5
return 0;
}
Here is a little live example to illustrate this : http://ideone.com/I0Oj66
About the copy assignment operator= :
A copy assignment operator of class T is a non-template non-static member function with the name operator= that takes exactly one parameter of type T.
The operator= is used to change an existing object.
You can use it for example to copy the state of another object :
rational &operator=( const rational &rhs )
{
num = rhs.num;
den = rhs.den;
return *this;
}

overloading operator * c++

I have been trying to compile this program but it is giving me an error in regards to overloading the * operator for one of the functions: complex operator *(double n)const
When I try to compile I get the error: no match for 'operator*' in '2 * c'
Here is the header file:
Complex.h
#ifndef COMPLEX0_H
#define COMPLEX0_H
class complex {
double realNum;
double imagNum;
public:
complex();
complex(double x,double y);
complex operator *(double n)const;
complex operator *(const complex &c1)const;
friend std::istream &operator>>(std::istream &is,complex &cm);
friend std::ostream &operator<<(std::ostream &os,const complex &cm);
};
#endif
Here is the cpp:
Complex.cpp
#include "iostream"
#include "complex0.h"
complex::complex() {
imagNum = 0.0;
realNum = 0.0;
}
complex::complex(double x, double y) {
realNum = x;
imagNum = y;
}
complex complex::operator *(const complex& c1) const{
complex sum;
sum.realNum=realNum*c1.realNum-c1.imagNum*imagNum;
sum.imagNum=realNum*c1.imagNum+imagNum*c1.realNum;
return sum;
}
complex complex::operator *(double n)const{
complex sum;
sum.realNum=realNum*n;
sum.imagNum=imagNum*n;
return sum;
}
std::istream &operator >>(std::istream& is, complex& cm) {
is >> cm.realNum>> cm.imagNum;
return is;
}
std::ostream &operator <<(std::ostream& os, const complex& cm){
os<<"("<<cm.realNum<<","<<cm.imagNum<<"i)"<<"\n";
return os;
}
main.cpp
#include <iostream>
using namespace std;
#include "complex0.h"
int main() {
complex a(3.0, 4.0);
complex c;
cout << "Enter a complex number (q to quit):\n";
while (cin >> c) {
cout << "c is " << c << "\n";
cout << "a is " << a << "\n";
cout << "a * c" << a * c << "\n";
cout << "2 * c" << 2 * c << "\n";
cout << "Enter a complex number (q to quit):\n";
}
cout << "Done!\n";
return 0;
}
Can someone explain to me what I have done wrong?
The member function operator only applies when the first operand is of your class type. If you want to handle the case where the second operand is of your type, you need also a free function (in which we simply delegate to the member function by virtue of commutativity of the operation):
complex operator*(double n, complex const & x)
{
return x * n;
}
(Please note that the standard library already contains <complex>.)
You have a member function defined as follows:
complex complex::operator *(double n) const;
That will let you do things like: complex_number * 3.0, but not 3.0 * complex_number. However, you can't create a member function that will let you do 3.0 * complex_number. The only place you could create that member function, is inside the definition of double, which you can't change.
Instead of doing it as member functions though, you can also do it as free-standing functions:
complex operator*(complex x, double n); // Called for complex_number * 2.0
complex operator*(double n, complex x); // Called for 2.0 * complex_number

OOP C++ first compiler error

Please help this newbie, here's my code:
#include <iostream>
using namespace std;
class Complex {
private:
float r, i;
public:
Complex(float rr, float ii) : r(rr), i (ii) {}
float GiveRe () { return r; }
float GiveIm () { return i; }
void Setit (float rr, float ii) {
r = rr;
i = ii;
}
};
Complex a(10, 20);
Complex sumit (Complex &ref) {
static Complex sum (0, 0);
sum.Setit(sum.GiveRe() + ref.GiveRe(), sum.GiveIm() + ref.GiveIm());
return sum;
}
int main () {
Complex sumvalue = sumit (a);
cout << sumvalue << endl;
return 0;
}
error: no match for 'operator<<' in 'std::cout << sumvalue'.
The program should output the sum of a complex number.
cout can't tell what you want to output, you need to specify the operator<< in the class or make it possible to implicitly convert your class to a compatible type.
http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/
Rudolf Mühlbauer's code as implemented in your class:
Add this somewhere within the class header:
friend ostream& operator<<(ostream& out, const Complex& compl);
and this below the header:
ostream& operator<<(ostream& out, const Complex& compl)
{
return out << compl.r << "/" << compl.i;
}
Implementation should be changed to suit your exact needs.
Complex doesn't have an operator <<
ostream& Complex::operator << ( ostream& os )
{
// use os << field/method here to out put
return os;
}
Also if complex can be displayed to console in different ways, then you should think of using methods to display instead of cout <<
void Complex::DisplayToConsole()
{
std::cout << r << " " << i << '\n';
}
You have to overload the "<<" operator for Complex type.
#include <iostream>
using namespace std;
class Complex {
private:
float r, i;
public:
Complex(float rr, float ii) : r(rr), i (ii) {}
float GiveRe () { return r; }
float GiveIm () { return i; }
void Setit (float rr, float ii) {
r = rr;
i = ii;
}
};
ostream& operator<<(ostream& os, Complex& c)
{
float i;
os<<c.GiveRe();
if(c.GiveIm() < 0){
os<<"-j"<<c.GiveIm()*(-1);
}else{
os<<"+j"<<c.GiveIm();
}
return os;
}
Complex a(10, 20);
Complex sumit (Complex &ref) {
static Complex sum (0, 0);
sum.Setit(sum.GiveRe() + ref.GiveRe(), sum.GiveIm() + ref.GiveIm());
return sum;
}
int main () {
Complex sumvalue = sumit (a);
cout << sumvalue << endl;
return 0;
}
A complete minimal example:
#include <iostream>
using namespace std;
class C {
public:
int r, l;
// if the operator needs access to private fields:
friend ostream& operator<< (ostream&, const C&);
};
ostream& operator << (ostream& stream, const C& c) {
stream << c.r << "--" << c.l;
return stream;
}
int main() {
C c;
c.l = 1;
c.r = 2;
cout << c << endl;
}
C++ allows you to define operators. The STL uses the << operator for output, and the whole istream/ostream class hierarchy uses this operator to input/output.
Operators are implemented as functions, but always follow a very specific syntax. As in the example, ostream& operator << (ostream&, const MYTYPEHERE&) is the way to define ostream << operators.
When C++ encounters a statement, it has to deduce the types of all operands, and find (quite magically, indeed) a solution to the question: given my operands and operators, can i find a typing such that the statement gets valid?
These ofstream operators are defined for all basic types somewhere in <iostream>, so if you write cout << 10, the compiler finds an operator ostream& operator<< (ostream&, int).
If you want to be able to use userdefined types in this game, you have to define the operators. otherwise, a statement cout << sometype will not be valid. This is also the reason for the harsh compiler errors sometimes found in C++: "Well, i have some operators << defined, but none is compatible with your type!".
Because sometimes it is not favourable to implement operators for your types (if you only output them once, e.g.), i suggested to write:
cout << sum.re << "--" << sum.im << endl; // or similar
This way, you write less code, and you are flexible in the output format. Who knows if you want you complex number formatted differently next time? But this is another discussion.
Why complicating that much? Because C++ can be awfully complicated. It it very powerfull, but crammed with special syntax and exceptions to those. In the end, the difference to C lies exactly here: C++ does a much better job with type inference (needed for templates), often resulting in WTF?
On how to implement it in your code, i think the other answers provide nice solutions!
sumit(a) returns an object of type Complex, which cout was not defined to handle.