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
  • 24,552
  • 19
  • 101
  • 135
user954004
  • 101
  • 1
  • 6
  • 1
    You need to review your questions and accept some answers! – karlphillip Nov 21 '11 at 23:08
  • This would help http://stackoverflow.com/questions/570669/checking-if-a-double-or-float-is-nan-in-c/570694#570694 – Hauleth Nov 21 '11 at 23:09
  • You need to define what would be outside the range of acceptable values and then check for that. – r_ahlskog Nov 21 '11 at 23:10
  • This question is not a duplicate of the indicated question. See this conversation on meta: http://meta.stackexchange.com/questions/109993/guidelines-for-closing-questions-as-exact-duplicate – John Dibling Nov 22 '11 at 14:50

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
  • 44,150
  • 20
  • 113
  • 196
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
  • 841
  • 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,242
  • 2
  • 23
  • 26