Seb
Seb

Reputation: 2016

Assert is wrong, proven with cout

When I run this, in main() the cout prints 5.395. But the assert says it failed!! This is really mind boggling, why is this happening?

#include <iostream>
#include <cassert>

using namespace std;

const float A = 1.6;
const float C = 1.55;
const float G = 2.2;
const float T = 1.23;

char empty[18];
int arraySize;


void copyArray(char sourceArray[], char targetArray[], int size) {
    for(int i=0;i<size;i++) {
        targetArray[i] = sourceArray[i];
        }
    }



double getAvgDensity(char aminoAcid) {

char aminoUpper = toupper(aminoAcid);
aminoToArray(aminoUpper);
    double counter = 0;
    int codonTotal = arraySize / 3.0;
    if (arraySize == 0)
        return 0;
    else
    {
    for (int i = 0; i < arraySize; i++) {
        counter += charToDouble(empty[i]);
        }

    return (counter / codonTotal);
    }

}


int main()
{
    cout << getAvgDensity('A') << endl;  // prints 5.395
    assert(getAvgDensity('A')==5.395);
    return 0;
}

Edit: Thanks for all the answers, I just multiplied by 1000, converted to int, converted back to double and divided by 1000. :)

Upvotes: 4

Views: 1706

Answers (3)

paddy
paddy

Reputation: 63481

This has already been answered, but I'll add my two cents. You may find it useful to make a function to compare doubles if you plan to do this often. The idea is to check that fabs(a-b) < epsilon where epsilon is a small value representing a tolerable amount of error.

bool is_equal( double a, double b, const double epsilon = 1e-5 )
{
    double c = a - b;
    return c < epsilon && -c < epsilon;  // or you could use fabs(c) < epsilon
}

Then it's just a case of doing this:

assert( is_equal(getAvgDensity('A'), 5.395) );

Upvotes: 1

user1952500
user1952500

Reputation: 6781

The reason is cout does not print to that high a precision by default. Try the following:

int main()
{
    std::cout.precision(25);
    cout << getAvgDensity('A') << endl;  // prints 5.395
    assert(getAvgDensity('A')==double(5.395));
    return 0;
}

Upvotes: 0

salezica
salezica

Reputation: 77099

Ah, floating point.

Say, for example, the actual return of getAvgDensity() is 5.395000000000000000000000001. It's not technically == 5.395, is it? Printing would, of course, discard all those pesky trailing decimals, but the value remains different.

When working with floats, you have to decide yourself what is an acceptable definition for "equal". Round the number manually, or compare it with <=/>= and suitable error margins.

Upvotes: 10

Related Questions