c++ - Assert is wrong, proven with cout -


when run this, in main() cout prints 5.395. assert says failed!! mind boggling, why happening?

#include <iostream> #include <cassert>  using namespace std;  const float = 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     {     (int = 0; < 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: answers, multiplied 1000, converted int, converted double , divided 1000. :)

ah, floating point.

say, example, actual return of getavgdensity() 5.395000000000000000000000001. it's not technically == 5.395, it? printing would, of course, discard pesky trailing decimals, value remains different.

when working floats, have decide acceptable definition "equal". round number manually, or compare <=/>= , suitable error margins.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -