Today I have just discovered the oddest bug that I have ever seen, which took me several hours to find. I've narrowed down the problem to a single line of code which simply is:
double cosAngle = Math.cos(angleInRadians);
For certain angles (or periods of time?) the function returns the wrong answer!
I'm certain that it's not due to incorrect conversion of degrees to radians or other common mistakes that people make with the cos function.
It only happens to my Nexus 4 device running 4.4.2 and I ran a little test from the main thread:
Log.d("test", "---------------- Beginning Test ---------------------- ");
for(int i = 0; i < 1000; i++) {
Log.d("test", "Math.cos(1.603635633038609d):\n" + Math.cos(1.603635633038609d));
}
When I print the solution to logcat I get:
D/test ( 7875): Math.cos(1.603635633038609d):
D/test ( 7875): -0.03283340413433994
D/test ( 7875): Math.cos(1.603635633038609d):
D/test ( 7875): -0.04844533640025519
And it randomly fluctuates between these two values. The correct answer being -0.0328.. Has anyone any insight as to what might be happening? Is this a software/hardware issue?
It has visual implications as it causes pixels to flicker and other weird effects.