C++ - If statement still execute when comparing doubles












1















In this project, I am expected to created a Polynomial class. Here's is what I have for the Polynomial.cpp file:



#include <iostream>
#include "Polynomial.h"
#include <stdexcept>
#include <iomanip>
#include <cmath>
#include <string>

using namespace std;

Polynomial::Polynomial(double c, int size){
set(c,size);
}

Polynomial::Polynomial(const Polynomial &poly){
size = poly.size;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = poly.c[i];
}
}

Polynomial::~Polynomial(){
delete c;
}

void Polynomial::set(double ca, int s){
if(s < 1){
throw std::invalid_argument("Size must be larger or equal to 1.");
}
else{
size = s;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = ca[i];
}
}
}

double* Polynomial::getPointer() const{
return c;
}

int Polynomial::getSize() const{
return size;
}

bool Polynomial::operator ==(const Polynomial& poly) const{
if(size != poly.size){
return false;
}
int i = 0;
int count = 0;
while ( i < size ) {
if ( c[i] == poly.c[i] ) {
count++;
}
i++;
}
if ( count != size) {
return false;
}
return true;
}

Polynomial Polynomial::operator +(const Polynomial& poly) const{
int d = fabs(size - poly.size); //absolute value of size difference
int bigger = size; //the size of the bigger polynomial
int smaller = poly.size; //the size of the smaller polynomial
if ( poly.size > size) {
bigger = poly.size;
smaller = size;
}
double r[bigger];
double s[bigger]; //new coef array for the smaller polynomial with zeros coefs added

if ( d != 0 ) {
if(smaller = poly.size){
for(int i=0; i < smaller; i++){
s[i] = poly.c[i];
}
for(int i=smaller; i < bigger; i++){
s[i] = 0;
}
for(int i=0; i < bigger; i++){
r[i] = c[i] + s[i];
}
}
else{
for(int i=0; i < smaller; i++){
s[i] = c[i];
}
for(int i=smaller; i < bigger; i++){
s[i] = 0;
}
for(int i=0; i < bigger; i++){
r[i] = poly.c[i] + s[i];
}
}
}
else {
for ( int i = 0; i < bigger; i++) {
r[i] = c[i] + poly.c[i];
}
}

return Polynomial(r,bigger);
}

ostream& operator<<(ostream &lhs, const Polynomial &poly){
string plus = "+";
bool valid = false; //check if the first term of the polynomial to be printed is valid or not, valid is true when term is non-zero

if(poly.getPointer()[poly.getSize()-1] < 0){
if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
}
for(int i=poly.getSize()-2; i >= 0; i--){
if(poly.getPointer()[i] != 0 && i != 1 && i != 0){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x^" << i << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x^" << i << " ";
}
}
else if(poly.getPointer()[i] != 0 && i == 1){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x" << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x" << " ";
}
}
else if(poly.getPointer()[i] != 0 && i == 0){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i];
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i];
}
}
}
}
else{
if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << noshowpos << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
valid = true;
}
for(int i=poly.getSize()-2; i >= 0; i--){
if(poly.getPointer()[i] != 0 && i != 1 && i != 0){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x^" << i << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x^" << i << " ";
valid = true;
}
}
else if(poly.getPointer()[i] != 0 && i == 1){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x" << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x" << " ";
valid = true;
}
}
else if(poly.getPointer()[i] != 0 && i == 0){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i];
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i];
valid = true;
}
}
}
}
return lhs;


}



Here is my test program:



#include <iostream>
#include "Polynomial.h"
#include <iomanip>

using namespace std;

int main() {
double c = {0,-5,3};
double d = {3,0,-5,0};

Polynomial p1(c,3);
Polynomial p2(d,4);
cout << "Polynomial p1 is: " << p1 << endl;
cout << "Degree of polynomial p1 is: " << noshowpos << p1.getDegree() << endl;
cout << "Polynomial p2 is: " << p2 << endl;

Polynomial p3(p1);
cout << "Polynomial p3 now is equal to p1 after copy constructor: " << p3 << endl;

bool same = p1 == p3;
cout << "Is p1 equal to p3? " << noshowpos << same << endl;

Polynomial p4 = p1 + p2;
cout << "Polynomial p4 is the sum of p1 and p2: " << p4 << endl;


So the problem I am having is with this if statement:



if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
}


When I perform the operator+ for p4 = p1 + p2, the Polynomial array of double for p4 is [3, -5, -2, 0]. So the last value of p4[size - 1] of this array is 0. As you can see above in the if statement, when I get the poly.getPointer()[poly.getSize()-1] value, it equals to 0; however when I compare it with the zero in the if statement, it still executes even though the if statement is false (0 is not equal to 0 so it should skip this if statement). I have tried running it both on an IDE and on a Linux engine; however, the result is still the same. I am not sure if it is related to this constructor which calls the set method:



Polynomial::Polynomial(double c, int size){ 
set(c,size);
}

void Polynomial::set(double ca, int s){
if(s < 1){
throw std::invalid_argument("Size must be larger or equal to 1.");
}
else{
size = s;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = ca[i];
}
}
}


Please help. Thank you.










share|improve this question


















  • 4





    You can't really compare floating point values for exact equality on computers. See e.g. Is floating point math broken? for some details

    – Some programmer dude
    Nov 17 '18 at 5:15











  • What is the most effective way for float and double comparison?, How dangerous is it to compare floating point values?, c++ comparison of two double values not working properly, Why doesn't my floating-point comparison work? floating-point-gui.de

    – phuclv
    Nov 17 '18 at 6:22













  • @Someprogrammerdude If you can't compare values, you can do computations either.

    – curiousguy
    Nov 17 '18 at 14:18











  • @curiousguy Comparisons for exact equality I said. Comparisons in general (like larger/less than, or equality with an epsilon) are just fine. And on the subject of computations, the more of them you do with floating point values, the more inexact they become.

    – Some programmer dude
    Nov 17 '18 at 14:33











  • @Someprogrammerdude If you can't compare for equality, the compiler is broken. So you can't compare at all.

    – curiousguy
    Nov 17 '18 at 14:35
















1















In this project, I am expected to created a Polynomial class. Here's is what I have for the Polynomial.cpp file:



#include <iostream>
#include "Polynomial.h"
#include <stdexcept>
#include <iomanip>
#include <cmath>
#include <string>

using namespace std;

Polynomial::Polynomial(double c, int size){
set(c,size);
}

Polynomial::Polynomial(const Polynomial &poly){
size = poly.size;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = poly.c[i];
}
}

Polynomial::~Polynomial(){
delete c;
}

void Polynomial::set(double ca, int s){
if(s < 1){
throw std::invalid_argument("Size must be larger or equal to 1.");
}
else{
size = s;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = ca[i];
}
}
}

double* Polynomial::getPointer() const{
return c;
}

int Polynomial::getSize() const{
return size;
}

bool Polynomial::operator ==(const Polynomial& poly) const{
if(size != poly.size){
return false;
}
int i = 0;
int count = 0;
while ( i < size ) {
if ( c[i] == poly.c[i] ) {
count++;
}
i++;
}
if ( count != size) {
return false;
}
return true;
}

Polynomial Polynomial::operator +(const Polynomial& poly) const{
int d = fabs(size - poly.size); //absolute value of size difference
int bigger = size; //the size of the bigger polynomial
int smaller = poly.size; //the size of the smaller polynomial
if ( poly.size > size) {
bigger = poly.size;
smaller = size;
}
double r[bigger];
double s[bigger]; //new coef array for the smaller polynomial with zeros coefs added

if ( d != 0 ) {
if(smaller = poly.size){
for(int i=0; i < smaller; i++){
s[i] = poly.c[i];
}
for(int i=smaller; i < bigger; i++){
s[i] = 0;
}
for(int i=0; i < bigger; i++){
r[i] = c[i] + s[i];
}
}
else{
for(int i=0; i < smaller; i++){
s[i] = c[i];
}
for(int i=smaller; i < bigger; i++){
s[i] = 0;
}
for(int i=0; i < bigger; i++){
r[i] = poly.c[i] + s[i];
}
}
}
else {
for ( int i = 0; i < bigger; i++) {
r[i] = c[i] + poly.c[i];
}
}

return Polynomial(r,bigger);
}

ostream& operator<<(ostream &lhs, const Polynomial &poly){
string plus = "+";
bool valid = false; //check if the first term of the polynomial to be printed is valid or not, valid is true when term is non-zero

if(poly.getPointer()[poly.getSize()-1] < 0){
if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
}
for(int i=poly.getSize()-2; i >= 0; i--){
if(poly.getPointer()[i] != 0 && i != 1 && i != 0){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x^" << i << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x^" << i << " ";
}
}
else if(poly.getPointer()[i] != 0 && i == 1){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x" << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x" << " ";
}
}
else if(poly.getPointer()[i] != 0 && i == 0){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i];
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i];
}
}
}
}
else{
if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << noshowpos << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
valid = true;
}
for(int i=poly.getSize()-2; i >= 0; i--){
if(poly.getPointer()[i] != 0 && i != 1 && i != 0){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x^" << i << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x^" << i << " ";
valid = true;
}
}
else if(poly.getPointer()[i] != 0 && i == 1){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x" << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x" << " ";
valid = true;
}
}
else if(poly.getPointer()[i] != 0 && i == 0){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i];
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i];
valid = true;
}
}
}
}
return lhs;


}



Here is my test program:



#include <iostream>
#include "Polynomial.h"
#include <iomanip>

using namespace std;

int main() {
double c = {0,-5,3};
double d = {3,0,-5,0};

Polynomial p1(c,3);
Polynomial p2(d,4);
cout << "Polynomial p1 is: " << p1 << endl;
cout << "Degree of polynomial p1 is: " << noshowpos << p1.getDegree() << endl;
cout << "Polynomial p2 is: " << p2 << endl;

Polynomial p3(p1);
cout << "Polynomial p3 now is equal to p1 after copy constructor: " << p3 << endl;

bool same = p1 == p3;
cout << "Is p1 equal to p3? " << noshowpos << same << endl;

Polynomial p4 = p1 + p2;
cout << "Polynomial p4 is the sum of p1 and p2: " << p4 << endl;


So the problem I am having is with this if statement:



if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
}


When I perform the operator+ for p4 = p1 + p2, the Polynomial array of double for p4 is [3, -5, -2, 0]. So the last value of p4[size - 1] of this array is 0. As you can see above in the if statement, when I get the poly.getPointer()[poly.getSize()-1] value, it equals to 0; however when I compare it with the zero in the if statement, it still executes even though the if statement is false (0 is not equal to 0 so it should skip this if statement). I have tried running it both on an IDE and on a Linux engine; however, the result is still the same. I am not sure if it is related to this constructor which calls the set method:



Polynomial::Polynomial(double c, int size){ 
set(c,size);
}

void Polynomial::set(double ca, int s){
if(s < 1){
throw std::invalid_argument("Size must be larger or equal to 1.");
}
else{
size = s;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = ca[i];
}
}
}


Please help. Thank you.










share|improve this question


















  • 4





    You can't really compare floating point values for exact equality on computers. See e.g. Is floating point math broken? for some details

    – Some programmer dude
    Nov 17 '18 at 5:15











  • What is the most effective way for float and double comparison?, How dangerous is it to compare floating point values?, c++ comparison of two double values not working properly, Why doesn't my floating-point comparison work? floating-point-gui.de

    – phuclv
    Nov 17 '18 at 6:22













  • @Someprogrammerdude If you can't compare values, you can do computations either.

    – curiousguy
    Nov 17 '18 at 14:18











  • @curiousguy Comparisons for exact equality I said. Comparisons in general (like larger/less than, or equality with an epsilon) are just fine. And on the subject of computations, the more of them you do with floating point values, the more inexact they become.

    – Some programmer dude
    Nov 17 '18 at 14:33











  • @Someprogrammerdude If you can't compare for equality, the compiler is broken. So you can't compare at all.

    – curiousguy
    Nov 17 '18 at 14:35














1












1








1


1






In this project, I am expected to created a Polynomial class. Here's is what I have for the Polynomial.cpp file:



#include <iostream>
#include "Polynomial.h"
#include <stdexcept>
#include <iomanip>
#include <cmath>
#include <string>

using namespace std;

Polynomial::Polynomial(double c, int size){
set(c,size);
}

Polynomial::Polynomial(const Polynomial &poly){
size = poly.size;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = poly.c[i];
}
}

Polynomial::~Polynomial(){
delete c;
}

void Polynomial::set(double ca, int s){
if(s < 1){
throw std::invalid_argument("Size must be larger or equal to 1.");
}
else{
size = s;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = ca[i];
}
}
}

double* Polynomial::getPointer() const{
return c;
}

int Polynomial::getSize() const{
return size;
}

bool Polynomial::operator ==(const Polynomial& poly) const{
if(size != poly.size){
return false;
}
int i = 0;
int count = 0;
while ( i < size ) {
if ( c[i] == poly.c[i] ) {
count++;
}
i++;
}
if ( count != size) {
return false;
}
return true;
}

Polynomial Polynomial::operator +(const Polynomial& poly) const{
int d = fabs(size - poly.size); //absolute value of size difference
int bigger = size; //the size of the bigger polynomial
int smaller = poly.size; //the size of the smaller polynomial
if ( poly.size > size) {
bigger = poly.size;
smaller = size;
}
double r[bigger];
double s[bigger]; //new coef array for the smaller polynomial with zeros coefs added

if ( d != 0 ) {
if(smaller = poly.size){
for(int i=0; i < smaller; i++){
s[i] = poly.c[i];
}
for(int i=smaller; i < bigger; i++){
s[i] = 0;
}
for(int i=0; i < bigger; i++){
r[i] = c[i] + s[i];
}
}
else{
for(int i=0; i < smaller; i++){
s[i] = c[i];
}
for(int i=smaller; i < bigger; i++){
s[i] = 0;
}
for(int i=0; i < bigger; i++){
r[i] = poly.c[i] + s[i];
}
}
}
else {
for ( int i = 0; i < bigger; i++) {
r[i] = c[i] + poly.c[i];
}
}

return Polynomial(r,bigger);
}

ostream& operator<<(ostream &lhs, const Polynomial &poly){
string plus = "+";
bool valid = false; //check if the first term of the polynomial to be printed is valid or not, valid is true when term is non-zero

if(poly.getPointer()[poly.getSize()-1] < 0){
if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
}
for(int i=poly.getSize()-2; i >= 0; i--){
if(poly.getPointer()[i] != 0 && i != 1 && i != 0){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x^" << i << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x^" << i << " ";
}
}
else if(poly.getPointer()[i] != 0 && i == 1){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x" << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x" << " ";
}
}
else if(poly.getPointer()[i] != 0 && i == 0){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i];
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i];
}
}
}
}
else{
if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << noshowpos << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
valid = true;
}
for(int i=poly.getSize()-2; i >= 0; i--){
if(poly.getPointer()[i] != 0 && i != 1 && i != 0){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x^" << i << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x^" << i << " ";
valid = true;
}
}
else if(poly.getPointer()[i] != 0 && i == 1){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x" << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x" << " ";
valid = true;
}
}
else if(poly.getPointer()[i] != 0 && i == 0){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i];
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i];
valid = true;
}
}
}
}
return lhs;


}



Here is my test program:



#include <iostream>
#include "Polynomial.h"
#include <iomanip>

using namespace std;

int main() {
double c = {0,-5,3};
double d = {3,0,-5,0};

Polynomial p1(c,3);
Polynomial p2(d,4);
cout << "Polynomial p1 is: " << p1 << endl;
cout << "Degree of polynomial p1 is: " << noshowpos << p1.getDegree() << endl;
cout << "Polynomial p2 is: " << p2 << endl;

Polynomial p3(p1);
cout << "Polynomial p3 now is equal to p1 after copy constructor: " << p3 << endl;

bool same = p1 == p3;
cout << "Is p1 equal to p3? " << noshowpos << same << endl;

Polynomial p4 = p1 + p2;
cout << "Polynomial p4 is the sum of p1 and p2: " << p4 << endl;


So the problem I am having is with this if statement:



if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
}


When I perform the operator+ for p4 = p1 + p2, the Polynomial array of double for p4 is [3, -5, -2, 0]. So the last value of p4[size - 1] of this array is 0. As you can see above in the if statement, when I get the poly.getPointer()[poly.getSize()-1] value, it equals to 0; however when I compare it with the zero in the if statement, it still executes even though the if statement is false (0 is not equal to 0 so it should skip this if statement). I have tried running it both on an IDE and on a Linux engine; however, the result is still the same. I am not sure if it is related to this constructor which calls the set method:



Polynomial::Polynomial(double c, int size){ 
set(c,size);
}

void Polynomial::set(double ca, int s){
if(s < 1){
throw std::invalid_argument("Size must be larger or equal to 1.");
}
else{
size = s;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = ca[i];
}
}
}


Please help. Thank you.










share|improve this question














In this project, I am expected to created a Polynomial class. Here's is what I have for the Polynomial.cpp file:



#include <iostream>
#include "Polynomial.h"
#include <stdexcept>
#include <iomanip>
#include <cmath>
#include <string>

using namespace std;

Polynomial::Polynomial(double c, int size){
set(c,size);
}

Polynomial::Polynomial(const Polynomial &poly){
size = poly.size;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = poly.c[i];
}
}

Polynomial::~Polynomial(){
delete c;
}

void Polynomial::set(double ca, int s){
if(s < 1){
throw std::invalid_argument("Size must be larger or equal to 1.");
}
else{
size = s;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = ca[i];
}
}
}

double* Polynomial::getPointer() const{
return c;
}

int Polynomial::getSize() const{
return size;
}

bool Polynomial::operator ==(const Polynomial& poly) const{
if(size != poly.size){
return false;
}
int i = 0;
int count = 0;
while ( i < size ) {
if ( c[i] == poly.c[i] ) {
count++;
}
i++;
}
if ( count != size) {
return false;
}
return true;
}

Polynomial Polynomial::operator +(const Polynomial& poly) const{
int d = fabs(size - poly.size); //absolute value of size difference
int bigger = size; //the size of the bigger polynomial
int smaller = poly.size; //the size of the smaller polynomial
if ( poly.size > size) {
bigger = poly.size;
smaller = size;
}
double r[bigger];
double s[bigger]; //new coef array for the smaller polynomial with zeros coefs added

if ( d != 0 ) {
if(smaller = poly.size){
for(int i=0; i < smaller; i++){
s[i] = poly.c[i];
}
for(int i=smaller; i < bigger; i++){
s[i] = 0;
}
for(int i=0; i < bigger; i++){
r[i] = c[i] + s[i];
}
}
else{
for(int i=0; i < smaller; i++){
s[i] = c[i];
}
for(int i=smaller; i < bigger; i++){
s[i] = 0;
}
for(int i=0; i < bigger; i++){
r[i] = poly.c[i] + s[i];
}
}
}
else {
for ( int i = 0; i < bigger; i++) {
r[i] = c[i] + poly.c[i];
}
}

return Polynomial(r,bigger);
}

ostream& operator<<(ostream &lhs, const Polynomial &poly){
string plus = "+";
bool valid = false; //check if the first term of the polynomial to be printed is valid or not, valid is true when term is non-zero

if(poly.getPointer()[poly.getSize()-1] < 0){
if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
}
for(int i=poly.getSize()-2; i >= 0; i--){
if(poly.getPointer()[i] != 0 && i != 1 && i != 0){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x^" << i << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x^" << i << " ";
}
}
else if(poly.getPointer()[i] != 0 && i == 1){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x" << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x" << " ";
}
}
else if(poly.getPointer()[i] != 0 && i == 0){
if(poly.getPointer()[i] > 0){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i];
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i];
}
}
}
}
else{
if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << noshowpos << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
valid = true;
}
for(int i=poly.getSize()-2; i >= 0; i--){
if(poly.getPointer()[i] != 0 && i != 1 && i != 0){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x^" << i << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x^" << i << " ";
valid = true;
}
}
else if(poly.getPointer()[i] != 0 && i == 1){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i] << "x" << " ";
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i] << "x" << " ";
valid = true;
}
}
else if(poly.getPointer()[i] != 0 && i == 0){
if(poly.getPointer()[i] > 0 && valid == true){
lhs << setprecision(1) << fixed << plus << poly.getPointer()[i];
}
else{
lhs << setprecision(1) << fixed << poly.getPointer()[i];
valid = true;
}
}
}
}
return lhs;


}



Here is my test program:



#include <iostream>
#include "Polynomial.h"
#include <iomanip>

using namespace std;

int main() {
double c = {0,-5,3};
double d = {3,0,-5,0};

Polynomial p1(c,3);
Polynomial p2(d,4);
cout << "Polynomial p1 is: " << p1 << endl;
cout << "Degree of polynomial p1 is: " << noshowpos << p1.getDegree() << endl;
cout << "Polynomial p2 is: " << p2 << endl;

Polynomial p3(p1);
cout << "Polynomial p3 now is equal to p1 after copy constructor: " << p3 << endl;

bool same = p1 == p3;
cout << "Is p1 equal to p3? " << noshowpos << same << endl;

Polynomial p4 = p1 + p2;
cout << "Polynomial p4 is the sum of p1 and p2: " << p4 << endl;


So the problem I am having is with this if statement:



if(poly.getPointer()[poly.getSize()-1] != 0){
lhs << setprecision(1) << fixed << poly.getPointer()[poly.getSize()-1] << "x^" << poly.getSize()-1 << " ";
}


When I perform the operator+ for p4 = p1 + p2, the Polynomial array of double for p4 is [3, -5, -2, 0]. So the last value of p4[size - 1] of this array is 0. As you can see above in the if statement, when I get the poly.getPointer()[poly.getSize()-1] value, it equals to 0; however when I compare it with the zero in the if statement, it still executes even though the if statement is false (0 is not equal to 0 so it should skip this if statement). I have tried running it both on an IDE and on a Linux engine; however, the result is still the same. I am not sure if it is related to this constructor which calls the set method:



Polynomial::Polynomial(double c, int size){ 
set(c,size);
}

void Polynomial::set(double ca, int s){
if(s < 1){
throw std::invalid_argument("Size must be larger or equal to 1.");
}
else{
size = s;
c = new double[size];
for(int i=0; i<size; i++){
c[i] = ca[i];
}
}
}


Please help. Thank you.







c++ if-statement overloading operator-keyword






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 '18 at 5:13









AllenAllen

204




204








  • 4





    You can't really compare floating point values for exact equality on computers. See e.g. Is floating point math broken? for some details

    – Some programmer dude
    Nov 17 '18 at 5:15











  • What is the most effective way for float and double comparison?, How dangerous is it to compare floating point values?, c++ comparison of two double values not working properly, Why doesn't my floating-point comparison work? floating-point-gui.de

    – phuclv
    Nov 17 '18 at 6:22













  • @Someprogrammerdude If you can't compare values, you can do computations either.

    – curiousguy
    Nov 17 '18 at 14:18











  • @curiousguy Comparisons for exact equality I said. Comparisons in general (like larger/less than, or equality with an epsilon) are just fine. And on the subject of computations, the more of them you do with floating point values, the more inexact they become.

    – Some programmer dude
    Nov 17 '18 at 14:33











  • @Someprogrammerdude If you can't compare for equality, the compiler is broken. So you can't compare at all.

    – curiousguy
    Nov 17 '18 at 14:35














  • 4





    You can't really compare floating point values for exact equality on computers. See e.g. Is floating point math broken? for some details

    – Some programmer dude
    Nov 17 '18 at 5:15











  • What is the most effective way for float and double comparison?, How dangerous is it to compare floating point values?, c++ comparison of two double values not working properly, Why doesn't my floating-point comparison work? floating-point-gui.de

    – phuclv
    Nov 17 '18 at 6:22













  • @Someprogrammerdude If you can't compare values, you can do computations either.

    – curiousguy
    Nov 17 '18 at 14:18











  • @curiousguy Comparisons for exact equality I said. Comparisons in general (like larger/less than, or equality with an epsilon) are just fine. And on the subject of computations, the more of them you do with floating point values, the more inexact they become.

    – Some programmer dude
    Nov 17 '18 at 14:33











  • @Someprogrammerdude If you can't compare for equality, the compiler is broken. So you can't compare at all.

    – curiousguy
    Nov 17 '18 at 14:35








4




4





You can't really compare floating point values for exact equality on computers. See e.g. Is floating point math broken? for some details

– Some programmer dude
Nov 17 '18 at 5:15





You can't really compare floating point values for exact equality on computers. See e.g. Is floating point math broken? for some details

– Some programmer dude
Nov 17 '18 at 5:15













What is the most effective way for float and double comparison?, How dangerous is it to compare floating point values?, c++ comparison of two double values not working properly, Why doesn't my floating-point comparison work? floating-point-gui.de

– phuclv
Nov 17 '18 at 6:22







What is the most effective way for float and double comparison?, How dangerous is it to compare floating point values?, c++ comparison of two double values not working properly, Why doesn't my floating-point comparison work? floating-point-gui.de

– phuclv
Nov 17 '18 at 6:22















@Someprogrammerdude If you can't compare values, you can do computations either.

– curiousguy
Nov 17 '18 at 14:18





@Someprogrammerdude If you can't compare values, you can do computations either.

– curiousguy
Nov 17 '18 at 14:18













@curiousguy Comparisons for exact equality I said. Comparisons in general (like larger/less than, or equality with an epsilon) are just fine. And on the subject of computations, the more of them you do with floating point values, the more inexact they become.

– Some programmer dude
Nov 17 '18 at 14:33





@curiousguy Comparisons for exact equality I said. Comparisons in general (like larger/less than, or equality with an epsilon) are just fine. And on the subject of computations, the more of them you do with floating point values, the more inexact they become.

– Some programmer dude
Nov 17 '18 at 14:33













@Someprogrammerdude If you can't compare for equality, the compiler is broken. So you can't compare at all.

– curiousguy
Nov 17 '18 at 14:35





@Someprogrammerdude If you can't compare for equality, the compiler is broken. So you can't compare at all.

– curiousguy
Nov 17 '18 at 14:35












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348448%2fc-if-statement-still-execute-when-comparing-doubles%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348448%2fc-if-statement-still-execute-when-comparing-doubles%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini