# Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

### Dark GDK / dbSIN problem

Message
Posted: 29th Aug 2013 20:28
dbSIN(30)= 0.5
dbSIN(150)= 0.50000005960 As far as i remember this should be 0.5
dbSIN(210)= -0.499999970198 And This -0.5
dbSIN(330)= -0.500000178814 This also

The problem is that my game requires

if (dbSIN(30)==dbSIN(150))
//do something
else
//do something

Same for
dbSIN(210)==dbSIN(330)

And it always evaluates as false.
Its same for 60,120,240,300 and any other angle

Why is dbSIN so inaccurate.
Posted: 29th Aug 2013 21:59 Edited at: 29th Aug 2013 22:00
Quote: " Why is dbSIN so inaccurate. "

Trig functions are particularly tricky to evaluate, they usually use some sort of approximation function (be it a taylor-mclaren series or other methods). I wouldn't say that being off by 0.00000005960 can be categorized as "inaccurate".

Solutions I would recommend:

-Try using the sin() function in the <cmath> library, it might be better but probably not significantly

-Use subtraction and check the value, like so:

std::abs() is also found in <cmath>

here is the reference for the <cmath> library, as well as others. http://www.cplusplus.com/reference/cmath/

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Posted: 25th Nov 2013 03:30
The reason for this is because trig functions convert to factors of pi, which is irrational. So there are precision errors (even with doubles). I, too encountered a need for precision and consistency. So what I did was I initialized a sin table. And used and repeated the angle range where it was accurate to initialize the whole thing. Sin table lookups execute much, much faster than calculating it as functions for each lookup, too. And they really don't take much memory.

If your table only needs integer degrees, it's easy, just 360 elements long. If it needs more precision, then multiply its size by an integer factor, ideally a power of 2. Then you can shift the table index, for lookups, instead of the slower multiplying or dividing it.

To initialize your table, you may wish to use the sin() function rather than the dbSin() function. With sin(), you can pass in doubles instead of floats, which increases the precision.

#define _USE_MATH_DEFINES
#include <math.h>

value = sin(2.0*M_PI/360.0*angle);