tags:

views:

82

answers:

3

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

+1  A: 

Try this:

#include <limits>
if( variable == numeric_limits<float>::infinity() )
  return 1;
John Dibling
A: 

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
A: 

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