0

I'm using c++ in visual studio express to generate random expression trees for use in a genetic algorithm type of program.

Because they are random, the trees often generate (I'll call them exceptions, I'm not sure what they are)

Thanks to a suggestion by George, I turned the mask _MCW_EM on so that hardware interrupts are turned off. (the default)

So, the program runs uninterrupted, but some of the values returned are: -1.#INF, -1.#NAN, -1.#INV.

I don't know how to identify these so that I can throw an exeption:

if ( variable == -1.#INF) ??

DigitalRoss in this post seemed to have the solution, but as I understood it I couldn't make it work.

I've been looking all over the place for this simple bit of code, that I assumed would be used all

the time, but have had no luck.

thanks

Community
  • 1
  • 1
Peter Stewart
  • 2,857
  • 6
  • 28
  • 30
  • 2
    possible duplicate of [How do you check for infinite and indeterminate values in C++?](http://stackoverflow.com/questions/410853/how-do-you-check-for-infinite-and-indeterminate-values-in-c) – kennytm Jun 15 '10 at 16:36

3 Answers3

1

Try this:

#include <limits>
if( variable == numeric_limits<float>::infinity() )
  return 1;
John Dibling
  • 99,718
  • 31
  • 186
  • 324
0

Thanks to KennyTM for spotting the duplicate. A link in the link answered my query.

I used:

#include "limits.h"

#include "math.h"

bool isIndeterminate(const double pV) 
{ 
    return (pV != pV); 
};  

bool isInfinite(const double pV) 
{ 
    return (pV >= DBL_MAX || pV <= -DBL_MAX); 
}; 

As KennyTM's response was as a comment, I'm (perhaps a little presumtiously) answering my own question.

Peter Stewart
  • 2,857
  • 6
  • 28
  • 30
0

On Windows you can use the api calls "_isnan()" and "_finite()".

http://msdn.microsoft.com/en-us/library/aa298428%28VS.60%29.aspx
http://msdn.microsoft.com/en-us/library/aa246875%28v=VS.60%29.aspx

Michael J
  • 7,631
  • 2
  • 24
  • 30