I’m observing differing behavior when porting to IBM Open XL C++ 17.1 (clang frontend) with this test:
#include <iostream>
#include <limits>
#include <sstream>
#define FLT_INF (std::numeric_limits<float>::infinity())
int main() {
std::stringbuf sb;
float val = -FLT_INF;
// write
std::ostream ostr_(&sb);
ostr_.setf(std::ios::scientific, std::ios::floatfield);
ostr_.precision(17);
ostr_ << val << "n";
// verify we can read from the hard-coded stream data
float tmp = 0.f;
std::istream istr_(&sb);
istr_ >> tmp;
std::cout << val << std::endl;
std::cout << tmp << std::endl;
std::cout << std::boolalpha << istr_.fail() << std::endl;
return 0;
}
Using Linux (GCC 11.4.1) I see the following behavior (note I also see this same behavior from Visual Studio 2022):
-inf
0
true
But on the AIX using IBM Open XL C++ 17.1 I see:
-INF
-INF
false
Notice on AIX the infinity value was successfully read from the stream and the fail state was not set.
At first I thought this was a bug with the AIX compiler but the small amount of reading I’ve done so far I’m not sure why all the other platforms don’t handle it. It seems like a legit value std::strtof
can handle (which at least based on what I’ve read is what basic_istream::operator>>
calls (via std::num_get
under the covers).
I’m near the extent of my C++ understandings. Can any C++ gurus let me know what the standard calls for behavior wise? Are both compilers right?
Thank you for your help.
1