Search code examples
c++qtqtcoreqdebugqtmath

qSin() giving wrong sin values?


I have the code:

for(int i = 0; i < 314; i++){
    float nextSine = aSin(i/5);
    qDebug() << "i: "<<QString::number(i)<<" sin(i/5) = nextSine: "<<nextSine;
}

And this is a sample of the results I'm getting:

i:  "303"  sin(i/5) = nextSine:  -0.304811
i:  "304"  sin(i/5) = nextSine:  -0.304811
i:  "305"  sin(i/5) = nextSine:  -0.966118
i:  "306"  sin(i/5) = nextSine:  -0.966118

It's right some of the times, but it's wrong other times, for instance when i==303


Solution

  • You probably want to cast to float before running the function:

    float nextSine = aSin((float)i/5.0);