1

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;   
}
Mateen Ulhaq
  • 21,459
  • 16
  • 82
  • 123
user954004
  • 101
  • 1
  • 5

3 Answers3

3

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.

Walter
  • 42,785
  • 19
  • 106
  • 187
1

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.

Clement Bellot
  • 823
  • 1
  • 7
  • 19
0

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;   
}
Gyan aka Gary Buyn
  • 12,052
  • 2
  • 22
  • 26