What is the syntax for undefined?

Oct 23, 2010 at 2:41am
Hey guys I can't seem to find the syntax for undefined. For example if I need to say if(x == undefined) or something like that. It is really bothering. Thanks for the help!
Oct 23, 2010 at 2:47am
There is no such thing, because if you have x, then x is defined, otherwise it's a compiler error.

Unless you mean the preprocessor in which case it's #ifndef

1
2
3
4
5
#ifndef x  // if x has not been #defined

  // .. conditionally compiled code

#endif 


but note that preprocessor conditionals are different from normal if statements.
Last edited on Oct 23, 2010 at 2:47am
Oct 23, 2010 at 3:09am
is there a way to say something that is being divided by zero? for example if(x == defined) where x = 5/0. then dose x have a value if it is being divided by zero?
Oct 23, 2010 at 4:24am
closed account (1yR4jE8b)
if you divide something by zero in your program, it will crash. So x can never have the value of 5/0.
Oct 23, 2010 at 8:38am
You have to tell the program not to divide by zero.

Example:

Say x = num1 / num2

Then you write something like

1
2
3
4
if (num2 != 0)
x = num1 / num2;
else
cout << "Error.  Dividing by zero is undefined." << endl;


Hope that's what you're looking for.

Oct 23, 2010 at 9:53am
Your code please.
Oct 23, 2010 at 12:00pm
darkestfright wrote:
if you divide something by zero in your program, it will crash. So x can never have the value of 5/0.

Really now?
Then why does this compile?
1
2
3
4
5
#include <iostream>
int main()
{   std::cout << 5.0/0.0;
    return 0;
} // main 
Oct 23, 2010 at 12:16pm
Your best choice is to use the Boost Math Toolkit's Special Functions: fpclassify
http://www.boost.org/doc/libs/1_44_0/libs/math/doc/sf_and_dist/html/math_toolkit/utils/fpclass.html

It does the right thing on all platforms, and is a header-only (I think). Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iomanip>
#include <iostream>
#include <boost/math/special_functions/fpclassify.hpp>
using namespace std;

void display( const char* name, double d )
  {
  cout << left << setw( 16 ) << name << " :   ";
  cout << ((boost::math::isnan)( d ) ? "NaN" : "   ") << ",  ";
  cout << ((boost::math::isinf)( d ) ? ((d < 0) ? "-INF" : "+INF") : "    ") << ",   ";
  cout << ((boost::math::isnormal)( d ) ? "normal" : "      ") << ", ";
  cout << d << endl;
  }

#define DISPLAY( x ) display( #x, x )

int main()
  {
  double not_a_number     =  0.0/0.0;
  double infinite_number  =  5.0/0.0;
  double ninfinite_number = -5.0/0.0;
  double normal_number    =  5.0;

  cout << "NAME             : ISNAN, ISINF, ISNORMAL, VALUE\n";
  DISPLAY( not_a_number     );
  DISPLAY( infinite_number  );
  DISPLAY( ninfinite_number );
  DISPLAY( normal_number    );

  return 0;
  }
NAME             : ISNAN, ISINF, ISNORMAL, VALUE
not_a_number     :   NaN,      ,         , NaN
infinite_number  :      ,  +INF,         , Infinity
ninfinite_number :      ,  -INF,         , Infinity
normal_number    :      ,      ,   normal, 5

As you can see, your other option is simply to use a std::stringstream and try to convert the number to a string and check against "NaN" and "Infinity"...

Hope this helps.
Oct 23, 2010 at 12:18pm
Kyon wrote:
Really now?
Then why does this compile?
Because that is done by the compiler, not during the execution of your program. The compiler recognizes that as a special case and assigns the proper INF value to the variable. (In your example the variable is an unnamed temporary sent to cout.)

Hope this helps.
Topic archived. No new replies allowed.