The C++ 14 (§21.5) standard states that:
float stof(const string& str, size_t* idx = 0);
double stod(const string& str, size_t* idx = 0);
long double stold(const string& str, size_t* idx = 0);
Effects: the first two functions call strtod(str.c_str(), ptr)
and the third function calls strtold( str.c_str(), ptr)
. Each function returns the converted result, if any. The argument ptr
designates a pointer to an object internal to the function that is used to determine what to store at *idx
. If the function does not throw an exception and idx != 0
, the function stores in *idx
the index of the first unconverted element of str
.
In many cases therefore they will be the same, but the intermediate double
does open the potential for double rounding. E.g. if str = "1.0000000596046448"
, then the closest float
(assuming IEEE754 arithmetic) is 1.0000001f
, whereas the closest double
is exactly halfway between 1.0f
and 1.0000001f
, and so subsequent conversion to float
will round down to 1.0f
.
That's the theory at least. In practice, however, I am unable to recreate this: http://ideone.com/NMRy14