The following code ----- #include <limits> #include <iostream> int main () { double a = std::numeric_limits<double>::infinity(); double b = -1; std::cout << a << std::endl; std::cin >> b; std::cout << " " << b << " " << std::cin.fail() << std::endl; return 0; } ----- outputs ----- inf 0 1 ----- when the user types inf<return>, while one could expect ----- inf inf 0 ----- The same kind of behavior happens for -inf and nan.
I know that this is implementation-defined, and as such it is an acceptable behavior, but it would be nice if << and >> could be more symmetric. I don't know what conversion functions exactly are called by >>, but the __convert_to_v() functions in libstdc++-v3/config/locale/gnu/c_locale.cc contain lines like: else if (__v == numeric_limits<double>::infinity()) { __v = numeric_limits<double>::max(); __err = ios_base::failbit; } [this condition is not the one run by the above-mentioned code, as we get a value 0 instead of numeric_limits<double>::max(), but you get the idea] It seems strange that these perfectly appropriate floating-point values that can be given by strtod are discarded for no apparent reason. -- Summary: std::operator>> discards appropriate floating-point values (Inf, -Inf, NaN) Product: gcc Version: 4.4.4 Status: UNCONFIRMED Severity: enhancement Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: eric dot buchlin at ias dot u-psud dot fr http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45442