Float & # 8596; std :: string conversion alternative?

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

i.e:

  • fast
  • C ++ way ( std::string instead of char* )
  • safe (no risk of buffer overflows)
  • valid (returns NaN if the conversion cannot be completed)
  • no external library (independent)

I prefer this more, a simple function, optimized and dot

cause:

  • atof and strtod is a C function and they do not return NaN after a crash, I prefer to work with std::string , so I just ask if someone already writes some wrapper to std::string that I can use (if you don't mind).
  • lexical_cast has a higher dependency
  • stringstream slow
  • sprintf has a buffer overflow risk and its C function
+8
c ++ string floating-point stl
source share
3 answers

I would look at Boost Spirit

At least the control values โ€‹โ€‹of the formatter (i.e. float โ†’ string) are sequentially displayed as the top of the count * 1 *

Also, the exact definition of the input format and semantics during parsing can be very well configured using the policy class.


Here is my absolute use of min-dependity qi :: any_real_parser <> and a list of dependencies that 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; } 

<sub>

  • promotion / concept
  • boost / configuration
  • boost / detail
  • pushing / dropping
  • increase / merge
  • push / iterator
  • boost / math
  • boost / mpl
  • nudge / optional
  • boost / preprocessor
  • boost / proto
  • gain / range
  • boost / regex
  • impulse / spirit
  • boost / typeof
  • boost / type _traits
  • enhancement / utility
  • boost / option

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

sub>

1 , for example. http://www.boost.org/doc/libs/1_47_0/libs/spirit/doc/html/spirit/karma/performance_measurements/numeric_performance/double_performance.html

+1
source share

If you want to convert from numeric types to std :: string, then there is a function std::to_string , available in the latest standard.

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

+1
source share

The fast format library should be able to perform the kind of conversions you are looking for, at least for writing a float. However, it does not handle float parsing.

0
source share

All Articles