Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 795

# 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?
0
1 Solution

Commented:

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 ---
0

Commented:
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".
0

Commented:
If you want more precision, why not use double?
0

Author Commented:
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...
0

Author Commented:
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...
0

Author Commented:
yah, I chose to just go with the double type instead. Thanx!
0

Commented:

double d = lexical_cast<double>(origString);

would be the correct way to call it ( it reads "convert origString to double and save it in the double type d" )

and, to set the precision, you'll need to modify lexical_cast() like this,

std::stringstream interpreter;
interpreter.precision(20);

and you should be ok
0

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.