I'm getting the slope of a line bounded by two points
float slopeXY(CGPoint p1, CGPoint p2)
{
return ((p2.y - p1.y) / (p2.x - p1.x));
}
If I give it a zero-sized line,
CGPoint p1 = CGPointMake(0, 10);
CGPoint p2 = CGPointMake(0, 10);
float sxy = slopeXY(p1, p2);
I don't get a divide by zero error.
With the default floating-point environment on OS X, floating-point division by zero does not cause a trap or exception. 0.0/0.0 will instead return a NaN and raise the invalid floating-point status flag in the fpscr. Dividing a non-zero value by 0.0 will return an infinity and raise the divide-by-zero flag.
You can check for these conditions, if you need to, using the isnan( ) and isinf( ) functions defined in math.h
Divide by zero error only happens for integer division. For float, normally you get infinity, unless the dividend is zero.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With