Page 1 of 3

Division on Zero

Posted: Fri Aug 27, 2010 1:17 pm
by ShiftZ
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?

Re: Division on Zero

Posted: Fri Aug 27, 2010 3:53 pm
by slembcke
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.

Re: Division on Zero

Posted: Sat Aug 28, 2010 5:23 am
by ShiftZ
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.

Re: Division on Zero

Posted: Sun Aug 29, 2010 8:57 pm
by slembcke
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.

Re: Division on Zero

Posted: Mon Aug 30, 2010 5:09 am
by ShiftZ
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.

Re: Division on Zero

Posted: Mon Aug 30, 2010 8:21 am
by slembcke
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.

Re: Division on Zero

Posted: Mon Aug 30, 2010 8:38 am
by ShiftZ
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.

Re: Division on Zero

Posted: Mon Aug 30, 2010 10:40 am
by slembcke
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);

Re: Division on Zero

Posted: Thu Sep 09, 2010 9:34 am
by ShiftZ
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;

Re: Division on Zero

Posted: Thu Sep 09, 2010 12:12 pm
by slembcke
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.