Cos function returning false value

Heya guys,

I'm working on a program right now but something that has worked for ever for me (never had any problems with it) is now not doing what I expect it to; I'm using cos() and I'm passing radian-values to it. The problem is that it returns -4.3711388e-008 instead of the expected 0.

Here's how I'm doing what I'm doing (Normally it would be all in 1 line ofcourse, but for debugging and testing purposes it's now seperated):

1
2
3
4
5
float rad = DegToRad(m_Rotation);    // m_Rotation is a float value ranging from -360 to 360.
float cosresult = cos(rad);

// My DegToRad() macro.
#define DegToRad(deg)			(deg * (PI/180)) 


Before anyone asks: I've already tried writing that conversion macro as a function but that's not the issue since it's still returning the same (false) value.
I couldn't find anything through Google except for 1 page where someone passed degrees to the function instead of radians but as you can see that's not the issue here.
Does anyone know why it's returning this or even how to fix it? I'm thinking it's a precision error (the value is relatively speaking very close 0), but still I wouldn't know how to fix that.

Thanks,

Rycul
Last edited on
You are most probably correct that it is a precision error. There are two ways to fix that:

1) don't use floating point (perhaps not an option in your case);
2) define an epsilon value e such that if the result is less than e, the result should be considered to be zero.

(Note that 2 is a common technique as well)

I'm thinking it's a precision error (the value is relatively speaking very close 0), but still I wouldn't know how to fix that.
You're right, it's a precision error. You can't fix it since floating point notation ( used by double and float ) doesn't hold exact values.
Thanks guys. I'll go see what I can do about this then.

EDIT: It's working properly now, thanks a bunch :)
Last edited on
Topic archived. No new replies allowed.