Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between std::stof and atof when each one should be used?

Tags:

c++

c++11

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,

  • can they be used interchangeably:
  • do they convert to same float value with same input?
  • what scenario is best to use for each of these?
like image 640
T M Avatar asked Sep 10 '19 06:09

T M


People also ask

What is std :: STOF?

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.

What is atof function?

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.

What library is atof in C++?

The C library function double atof(const char *str) converts the string argument str to a floating-point number (type double).

How do you convert a string to a float in C++?

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 .


2 Answers

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)
  • if the converted value falls outside of the range of a 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)
  • if no conversion can be performed, 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.

like image 62
Sander De Dycker Avatar answered Oct 22 '22 13:10

Sander De Dycker


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:

  • You have a conforming C++ 11 or later compiler, and
  • Your code can tolerate exceptions (or you're willing to catch them around every call), and
  • You already have a std::string or the performance hit of a conversion is unimportant.
  • Or, if you are a C++ beginner and don't know the answers to these questions.

Then I would suggest using std::stod. Otherwise, you could consider using atof.

like image 2
Rob L Avatar answered Oct 22 '22 15:10

Rob L