?
Solved

converting from string to float without loss of precision ??

Posted on 2003-02-26
7
Medium Priority
?
689 Views
Last Modified: 2008-02-01
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
Comment
Question by:jade03
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 7

Expert Comment

by:burcarpat
ID: 8027506

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
 

Expert Comment

by:JZ4096
ID: 8027785
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
 
LVL 11

Expert Comment

by:cup
ID: 8031691
If you want more precision, why not use double?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jade03
ID: 8035119
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 Comment

by:jade03
ID: 8035615
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 Comment

by:jade03
ID: 8037891
yah, I chose to just go with the double type instead. Thanx!
0
 
LVL 7

Accepted Solution

by:
burcarpat earned 150 total points
ID: 8058717

    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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question