How to convert string (containing double max) to double
I have no pr开发者_如何学JAVAoblem converting "normal" double values, but I can't convert numeric_limits<double>::max(
) or DBL_MAX
string representations?
std::string max = "1.79769313486232e+308";
std::istringstream stream(max);
double value;
// enters here, failbit is set
if (!(stream >> value))
Could it be something like the actual value of DBL_MAX
isn't exactly representable in exponential notation with 16 decimal places (say the decimal value is very slightly larger than the two based represenation) but initializing a double
with the DBL_MAX
will nevertheless set the correct value (due to rounding). std::istringstream
may be a little bit more pickish.
EDIT: Actually I found that the value of DBL_MAX
in my compiler is 1.7976931348623158e+308 which works fine for me to stream. Your number is rounded and slightly larger, hence the failure.
EDIT2: The exact value of DBL_MAX
in decimal form is given by (2 ^ (1023 - 52)) * (2 ^ 53 - 1)
which isn't representable with 16 decimal places.
if you use
double v = 1.79769313486232e+308;
cout << v << endl;
it would give you inf. that means the literal number is actually exceeds double now.
精彩评论