Link to home
Start Free TrialLog in
Avatar of jade03
jade03

asked on

converting from string to float without loss of precision ??

I read some stuff from an input file, saved it as a type string, did somethings to it, then tried converting it back to a float by doing:

float finalNum = atof( (char*)origString.c_str())

the original number is something like: -13679.0978570361
the final number after the conversion is: -13679.1

How can I avoid the loss of precisions?
Avatar of burcarpat
burcarpat


here's how boost's lexical_cast solves the problem.  i stripped out certain irrelevant parts to shorten the code.  usage:

    int i = lexical_cast<int>("123");

note that, this works the other way, too; i.e.;

    std::string s = lexical_cast<std::string>(123);

you'll need to #include <sstream> to make it work

you can adjust the precision via the precision() function of the stringstream

p.s.: for boost's original lexical_cast, see

    http://www.boost.org/libs/conversion/lexical_cast.htm

'hope this helps...

--- cut from here ---
#include <sstream>

template <
  typename Target,
  typename Source
  > 
Target
lexical_cast(Source arg) {

  std::stringstream interpreter;

  Target result;

  if (!(interpreter << arg) ||
      !(interpreter >> result) ||
      !(interpreter >> std::ws).eof()) {
    // cast error.  handle it somehow
    // boost guys throw an exception here
  };

  return result;

}; // lexical cast
--- cut from here ---
I don't know whether this is relevant, but floats only hold 6 digits of precision anyway.  In other words,

float n = -13679.0978570361;
cout << n << endl;

prints "-13679.1".
If you want more precision, why not use double?
Avatar of jade03

ASKER

thanx guys, these are all great suggestions...so to incorporate them all, I would first, use lexical_cast to cast my original string to ostringstream, then set my desired number of precision, then use lexical_cast again to convert that ostringstream type to a double?

Ex:

string s;

// assume s has some value
ostringstream x = lexical_cast<ostringstream>(s);

s.precision(20);

double d = lexical_cast<ostringstream>(s);


When I try to compile that, I get an error at the point where I'm casting s to ostringstream....

I can't seem to find examples anywhere where ppl cast things to ostringstream though...wondering if I'm doing things right....

any ideas?

or should I simply go ahead and use atof on my original string to convert it to a double? But doubles only have up to 10 precision right? I would still lose the rest...
Avatar of jade03

ASKER

thanx guys, these are all great suggestions...so to incorporate them all, I would first, use lexical_cast to cast my original string to ostringstream, then set my desired number of precision, then use lexical_cast again to convert that ostringstream type to a double?

Ex:

string s;

// assume s has some value
ostringstream x = lexical_cast<ostringstream>(s);

s.precision(20);

double d = lexical_cast<ostringstream>(s);


When I try to compile that, I get an error at the point where I'm casting s to ostringstream....

I can't seem to find examples anywhere where ppl cast things to ostringstream though...wondering if I'm doing things right....

any ideas?

or should I simply go ahead and use atof on my original string to convert it to a double? But doubles only have up to 10 precision right? I would still lose the rest...
Avatar of jade03

ASKER

yah, I chose to just go with the double type instead. Thanx!
ASKER CERTIFIED SOLUTION
Avatar of burcarpat
burcarpat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial