I have read some documents about each, for example
http://www.cplusplus.com/reference/string/stof/
http://www.cplusplus.com/reference/cstdlib/atof/
I understand that atof
is part of <cstdlib>
and has const char*
as input parameter, and std::stof
is part of <string>
and has different input format.
But it's not clear,
float
value with same input?std::stof in C++ idx : Pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value. This parameter can also be a null pointer, in which case it is not used.
Description. The atof() function converts a character string to a double-precision floating-point value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type.
The C library function double atof(const char *str) converts the string argument str to a floating-point number (type double).
C++ string to float and double Conversion The easiest way to convert a string to a floating-point number is by using these C++11 functions: std::stof() - convert string to float. std::stod() - convert string to double. std::stold() - convert string to long double .
I assume you meant to compare std::atof
with std::stod
(both return double
).
Just comparing the two linked reference pages yields the following differences :
std::atof
takes a char*
, while std::stod
takes either a std::string
or a std::wstring
(ie. it has support for wide strings)std::stod
will also return the index of the first unconverted character if the pos
parameter is not NULL
(useful for further parsing of the string)double
, std::atof
will return an undefined value, while std::stod
will throw a std::out_of_range
exception (definitely better than an undefined value)std::atof
will return 0.0
, while std::stod
will throw a std::invalid_argument
exception (easier to distinguish with an actual converted 0.0
)These are all positive points for std::stod
, making it the more advanced alternative of the 2.
Everything Sander said is correct. However, you specifically asked:
Can they be used interchangeably?
The answer is no, at least not in the general case. If you're using atof, chances are good that you have legacy C code. In that case, you must be careful about introducing code that can throw exceptions, especially in "routine" situations such as when a user gives bad input.
Do they convert to same float
value with same input?
No. They both convert to a double
value, not a float. Conversion to the same value isn't specifically guaranteed by the standard (to my knowledge), and it is possible that there is round-off somewhere that is slightly different. However, given valid input, I would be pretty surprised if there were a difference between the return values from the two in the same compiler.
What scenario is best to use for each of these?
If:
Then I would suggest using std::stod. Otherwise, you could consider using atof.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With