Reputation: 99
I am trying to figure out a way to check for a undefined value of a slope in which case it would be vertical. I have tried using NULL
but that doesn't seem to work.
double Point::Slope(Point &p2)
{
double slop = 0;
slop = (y - p2.y) / (x - p2.x);
if (slop == NULL)
{
slop = 10e100;
}
return slop;
}
Upvotes: 1
Views: 6693
Reputation: 45474
If you mean nan ('not a number') with "undefined", you should avoid computing one in the first place, i.e. by checking that the denominator of a '/' operation is not zero. Second, you can always check for nan by
#include <cmath>
bool std::isnan(x); // since C++11
bool isnan(x); // pre C++11, from the C math library, defined as macro
see the man pages, or cppreference.
Upvotes: 3
Reputation: 12410
I'd recommend avoiding the divide-by-zero all together (by the way... why don't you call it slope
instead of slop
?):
double Point::Slope(Point&p2)
{
double slope = 0;
double xDelta = x - p2.x;
double yDelta = y - p2.y;
if (xDelta != 0)
{
slope = yDelta / xDelta;
}
return slope;
}
Upvotes: 0
Reputation: 841
In C++, NULL == 0. This is not what you seek.
Maybe this may help you : http://www.gnu.org/s/hello/manual/libc/Infinity-and-NaN.html
Try the isnan(float) function.
Upvotes: 1