当我运行它时,在 main() 中 cout 打印 5.395。但是断言说它失败了!这真是令人难以置信,为什么会发生这种情况?
#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;
}
编辑:感谢所有答案,我只是乘以 1000,转换为 int,转换回 double 并除以 1000。:)