Division on Zero

Discuss any Chipmunk bugs here.

Division on Zero

Postby ShiftZ » Fri Aug 27, 2010 1:17 pm

slembcke
Hi,

cpSpaceHash.c 512:

Code: Select all
cpFloat dt_dx = 1.0f/cpfabs(b.x - a.x), dt_dy = 1.0f/cpfabs(b.y - a.y);


Getting devision on zero.
Do you have any technical agreement about division on zero. Is it legal?
ShiftZ
 
Posts: 114
Joined: Sat Mar 07, 2009 7:23 am

Re: Division on Zero

Postby slembcke » Fri Aug 27, 2010 3:53 pm

Looks like you are trying to do a zero length segment query. I think the correct behavior in that case would be to ignore the query altogether. With the way segment queries work, it would be impossible to detect a hit with any shape.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Division on Zero

Postby ShiftZ » Sat Aug 28, 2010 5:23 am

slembcke wrote:Looks like you are trying to do a zero length segment query.


No,
b.x - a.x = 0.
Its just a segment parallel to y - axis. and

b.y - a.y = 0,
parallel to x - axis.

Zero length when both are equal to zero.
ShiftZ
 
Posts: 114
Joined: Sat Mar 07, 2009 7:23 am

Re: Division on Zero

Postby slembcke » Sun Aug 29, 2010 8:57 pm

Oh, I see what you are getting at. Handling horizonal and vertical lines is handled later in that function by this:
Code: Select all
   // fix NANs in horizontal directions
   cpFloat next_h = (temp_h ? temp_h*dt_dx : dt_dx);
   cpFloat next_v = (temp_v ? temp_v*dt_dy : dt_dy);


Well, division of a finite floating point number by zero gives you infinity. So yes, it's fine. In fact, Chipmunk uses this in a number of places to deal with infinite masses and such.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Division on Zero

Postby ShiftZ » Mon Aug 30, 2010 5:09 am

I noticed that the division by zero greatly stall iPhone processor. I had a bug once, an unexplainable lags on iPhone when player is dead. Problem was that maxSpeed of player was zero and it was divisor in some places. So i switched on floating point exceptions to catch such situations and catched problem described above.
ShiftZ
 
Posts: 114
Joined: Sat Mar 07, 2009 7:23 am

Re: Division on Zero

Postby slembcke » Mon Aug 30, 2010 8:21 am

Hrm. That's strange, I've never heard of that before. Division by zero to get infinity is the standard floating point behavior. It shouldn't be producing any floating point exceptions, but I guess I'm not totally surprised that it's handled in a way that produces a stall though. Floating division generally already stalls the CPU pipeline on most CPUs.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Division on Zero

Postby ShiftZ » Mon Aug 30, 2010 8:38 am

It shouldn't be producing any floating point exceptions


As far as i know cpu can call a signal processor to trap a signal. So its your decision does it generate C++ fp exception or not.
If you mean hardware exception, it will be generated on all kind of proccessors i know. Not sure about stall, but armv6 lagging hardly.
Maybe there is a way to control it, i dont know, ARM support suggests to avoid division by zero at all, and offers some ways to trap it.
ShiftZ
 
Posts: 114
Joined: Sat Mar 07, 2009 7:23 am

Re: Division on Zero

Postby slembcke » Mon Aug 30, 2010 10:40 am

Strange. Floating point divide by zero is not supposed to be considered exceptional. It seems a bit odd that ARM decided to make it optionally exceptional.

I made the following change in trunk:
Code: Select all
   cpFloat dx = cpfabs(b.x - a.x), dy = cpfabs(b.y - a.y);
   cpFloat dt_dx = (dx ? 1.0f/dx : INFINITY), dt_dy = (dy ? 1.0f/dy : INFINITY);
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Division on Zero

Postby ShiftZ » Thu Sep 09, 2010 9:34 am

Another catch in cpShape.c 292:

legal arguments values:

n = {1.0000000, -0.00016872486 }
a = (506.35046, 504.56598};
b = {506.35031 505.58569 }

Code: Select all
cpFloat an = cpvdot(a, n);
cpFloat bn = cpvdot(b, n);


an = -506.43561
bn = -506.43561

Code: Select all
cpFloat t = (d - an)/(bn - an); 


Div by zero exception;
ShiftZ
 
Posts: 114
Joined: Sat Mar 07, 2009 7:23 am

Re: Division on Zero

Postby slembcke » Thu Sep 09, 2010 12:12 pm

Wrapped an if around it. Looks like I have some other unneeded code in there too. I'l have to look into that at some point.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Next

Return to Bugs

Who is online

Users browsing this forum: No registered users and 1 guest

cron