Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Macros Are Evil
08-28-2015, 10:21 AM
Post: #1
Macros Are Evil
So I've spent some time today try to figure out why cars are still flying off in random directions when hitting curbs sometimes.

It turns out it is a bug in Bullet, to be more specific in the SIMD_DEGS_PER_RAD macro.

Code:
#define SIMD_PI           btScalar(3.1415926535897932384626433832795029)
#define SIMD_2_PI         btScalar(2.0) * SIMD_PI
#define SIMD_HALF_PI      (SIMD_PI * btScalar(0.5))
#define SIMD_RADS_PER_DEG (SIMD_2_PI / btScalar(360.0))
#define SIMD_DEGS_PER_RAD  (btScalar(360.0) / SIMD_2_PI)

The first one to spot it gets a virtual cookie. Big Grin

PS:
What is affected by this bug? Tire forces (cars flying off randomly and stuff) and AI steering calculations.
Find all posts by this user
Quote this message in a reply
08-28-2015, 10:31 AM (This post was last modified: 08-28-2015 10:32 AM by CrystalH.)
Post: #2
RE: Macros Are Evil
Hmm.
I'd say they use 2 Pi / 360.0 instead of Pi / 180.f
And yeah, they use 3 times double to float: once in btScalar(2.0) then in btScalar(360.0) and in PI define itself
and that forces to use float division on those 3 floats with already conversion errors.
My guess is that the result is different from just float( 3.1415926535897932384626433832795029 / 180.0) or even /180.f
But yeah, btScalar can be a double if ussing double precision with Bullet, then it probably is okay. But for that one would have to include bullet sources in project, since all distribs have it off probably (just single float).
Visit this user's website Find all posts by this user
Quote this message in a reply
08-28-2015, 10:56 AM
Post: #3
RE: Macros Are Evil
(08-28-2015 10:31 AM)CrystalH Wrote: In order to view links, you must have to reply to this thread.Hmm.
I'd say they use 2 Pi / 360.0 instead of Pi / 180.f
And yeah, they use 3 times double to float: once in btScalar(2.0) then in btScalar(360.0) and in PI define itself
and that forces to use float division on those 3 floats with already conversion errors.
My guess is that the result is different from just float( 3.1415926535897932384626433832795029 / 180.0) or even /180.f
But yeah, btScalar can be a double if ussing double precision with Bullet, then it probably is okay. But for that one would have to include bullet sources in project, since all distribs have it off probably (just single float).

Nope, precision is not the problem.
Find all posts by this user
Quote this message in a reply
08-28-2015, 11:41 AM (This post was last modified: 08-28-2015 11:50 AM by CrystalH.)
Post: #4
RE: Macros Are Evil
Ah heh okay I see now.
They didn't put () around the SIMD_2_PI define:
should be:
#define SIMD_2_PI (btScalar(2.0) * SIMD_PI)
without that the
SIMD_DEGS_PER_RAD (btScalar(360.0) / SIMD_2_PI)
becomes 360 / 2 * PI = 180 * PI instead of 360 / (2 * PI) = 180 / PI.

Well yeah but I won't say defines are evil, just need special care.
Same could be said about = and ==. E.g. you can write if (a = b) and loose a lot of time to get what's wrong if you don't know already.
Visit this user's website Find all posts by this user
Quote this message in a reply
08-29-2015, 03:12 AM (This post was last modified: 08-29-2015 03:14 AM by CrystalH.)
Post: #5
RE: Macros Are Evil
Another one of my favourites is when you e.g.:
Code:
    int i=0;
    #define aa(a)  if (a) {  ++i;  }
    if (1)
      aa(i);
    else
      aa(i);
and then get an else without matching if, since you can't put ; after aa.
Visit this user's website Find all posts by this user
Quote this message in a reply
08-29-2015, 09:23 AM
Post: #6
RE: Macros Are Evil
They are evil, because even seasoned developers happen to mess it up.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)