Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

float <-> std::string conversion alternative?

is there any alternative to atof, strtod, lexical_cast, stringstream or sprintf?

that is:

  1. fast
  2. C++ way (std::string instead of char*)
  3. safe (no buffer overrun risk)
  4. valid (return NaN if conversion couldn't be made)
  5. no external library (independent)

I prefer more like this , a simple function, optimized, and to the point

reason :

  • atof and strtod is C function and they are not returning NaN upon failure, I prefer working on std::string, so I just asking if anyone already writing some wrapper to std::string that I can use (if you don't mind).
  • lexical_cast has boost dependency
  • stringstream is slow
  • sprintf has buffer overflow risk and its C function
like image 972
uray Avatar asked Sep 25 '11 21:09

uray


People also ask

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

We can convert float and double to string using the C++11 std::to_string() function. For the older C++ compilers, we can use std::stringstream objects.

What does STOF mean in C++?

std::stof in C++Parses string interpreting its content as a floating-point number, which is returned as a value of type float. Syntax : float stof (const string& str, size_t* idx = 0); float stof (const wstring& str, size_t* idx = 0); Parameters : str : String object with the representation of a floating-point number.

What is atof C++?

The atof() function in C++ interprets the contents of a string as a floating point number and return its value as a double.


2 Answers

I'd look at Boost Spirit

  • http://www.boost.org/doc/libs/1_47_0/libs/spirit/doc/html/spirit/qi/reference/numeric/real.html

At least the benchmarks of the formatters (that is float -> string) consistently turn out as top-of-the-bill*1*

Also the exact input format specification and semantics when parsing can be configured very nicely using a policy class.


Here is my absolute min-dependency use of qi::any_real_parser<> and the list of dependendencies it touches:

#include <boost/spirit/include/qi_real.hpp>

namespace qi = boost::spirit::qi;

int main()
{
    const char input[] = "3.1415926";
    const char *f(input);
    const char *l(f+strlen(input));

    qi::any_real_parser<double> x;
    double parsed;
    x.parse(f, l, qi::unused, qi::unused, parsed);

    return 0;
}

  • boost/concept
  • boost/config
  • boost/detail
  • boost/exception
  • boost/fusion
  • boost/iterator
  • boost/math
  • boost/mpl
  • boost/optional
  • boost/preprocessor
  • boost/proto
  • boost/range
  • boost/regex
  • boost/spirit
  • boost/typeof
  • boost/type_traits
  • boost/utility
  • boost/variant

aligned_storage.hpp,assert.hpp,blank_fwd.hpp,blank.hpp,call_traits.hpp,checked_delete.hpp,concept_check.hpp,config.hpp,cstdint.hpp,current_function.hpp,foreach_fwd.hpp,foreach.hpp,get_pointer.hpp,implicit_cast.hpp,iterator.hpp,limits.hpp,math_fwd.hpp,next_prior.hpp,noncopyable.hpp,none.hpp,none_t.hpp,optional.hpp,ref.hpp,static_assert.hpp,swap.hpp,throw_exception.hpp,type.hpp,utility.hpp,variant.hpp,version.hpp

1 e.g. http://www.boost.org/doc/libs/1_47_0/libs/spirit/doc/html/spirit/karma/performance_measurements/numeric_performance/double_performance.html

like image 155
sehe Avatar answered Oct 20 '22 00:10

sehe


If you want to convert from numerical types to std::string there's a std::to_string function available in the latest standard.

Unfortunately as I've found out recently, in Visual Studio 2010 it is somewhat limited because there are only three overloads available for it; long double, long long, and unsigned long long. This causes issues when trying to use them from within templates.

like image 21
Daemin Avatar answered Oct 20 '22 00:10

Daemin