A way to get stringstream >> my_double under g ++ to give an infinite value or NaN?

I have a C ++ function that accepts string input. In function, I use stringstreamto extract double.

Later I check that double is not NaN or infinity. However, I cannot come up with a test input that will force the extraction operator to get any of these values.

Here is an important part of my function. Actual use in some factory classes.

double from_text(const std::string& word, bool& failed){
  double ret; failed = true;

  std::istringstream param_in(word);
  if(!(param_in >> ret)) { 
    return ret; }
  if(is_nan_or_infinity(ret)) { 
    //I want to get here
    return ret; }

  failed = false; return ret;
}

You can tell if you cannot do this, why bother?

I do not want to remove if, because I know that in some implementations like IBM , stream operators can create such values.

, , , 100% - , , , , . . , from_text, , .

Edit

, , std::istringstream(some_magic_string) >> my_double inf nan , g++ Ubuntu.

, :

#include "test_stuff.hpp"
#include "from_text.hpp"
///Maybe more magic goes here
bool failed;
from_text("my_special_nan_producing_input",failed)
test_assert(failed,true,"NAN producing input causes failure");
from_text("my_special_inf_producing_input",failed)
test_assert(failed,true,"Infinity producing input causes failure");
///Clean up from all the special magic

_text , ret, - NaN inf.

+3
2

, , , . "inf" "NaN" ostringstream, . :

  • ostringstream , .
  • , istringstream .
  • , from_text, "inf" "NaN", .

, , .

, !

+1

, - , ret (2 * ret!= ret) , . , ! std:: numeric_limits, .

0

All Articles