1

The following function converts a string to a double but the precision is not enough.

double stringToDouble(string s) {
    double d;
    stringstream ss(s); //turn the string into a stream
    ss >> d; //convert
    return d;
}

When called with stringToDouble("31.2458782523") the output is 31.2459.

Without using the Boost libraries is there a way to do this better? I want a higher degree of precision. As high as possible.

Pranjal
  • 299
  • 3
  • 10
  • 1
    The output is what it is because you haven't set your output precision accordingly, NOT because the method isn't precise enough. There are quite a few questions about this on SO. – us2012 Mar 09 '13 at 20:34
  • @BenjaminLindley Well, the question says that OP is unhappy that the 'output' is 31.2459, and that they're unhappy because they want higher precision. The answer is that their current method gives them all the precision they want, if only they print the resulting double correctly. So I'm inclined to think that it *does* help. – us2012 Mar 09 '13 at 20:50
  • @us2012: You're right, my mistake. – Benjamin Lindley Mar 09 '13 at 20:51
  • Thanks everyone for your comments – Pranjal Mar 09 '13 at 21:02

2 Answers2

3

The double is parsed correctly, but you're most likely seeing it with the wrong precision. How do you output it? Tailor the precision to your need and it will be ok.

Also, you should know that floating point numbers cannot be always accurately represented in memory, so you may end up with (tiny) rounding errors when using float or doubles. But you can usually safely ignore those unless you're planning to send a rocket to the moon.

Qortex
  • 7,087
  • 3
  • 42
  • 59
  • Thank you Mic. So instead of working with SO, if I perform multiple mathematical calculations on these numbers, it would still retain the precision level right (Ignoring the tiny rounding errors). While performing calculations do I have to worry about setting the precision as well? – Pranjal Mar 09 '13 at 20:49
  • @user1549727 No, as long as you're working with `double`s the arithmetic will happen at `double` precision. Be careful not to convert your doubles to floats or integers in intermediate steps though. – us2012 Mar 09 '13 at 20:54
  • Cool. makes sense. Thanks again, love this community – Pranjal Mar 09 '13 at 20:57
  • Just to clarify: yes, all calculation will be made in double precision, because that's the way data is stored internally. But when you output it, you can use whatever precision suits you. Compiler takes care of performing always best-effort arithmetic: suppose `int x` and `double y`, then `x/y` will be `double`. But be careful, `int x` and `int y` lead to `x/y` as `int` and this might not be what you want (e.g. `5/2 == 2` but `5./2 = 2.5`). – Qortex Mar 09 '13 at 22:10
2

You can use std::stod if you are using C++11.

stod = S tring to D ouble

double myDouble = std::stod(myString);

This should provide relatively decent accuracy.

If even higher precision is required, you could use std::stold, for long-doubles.

Jamin Grey
  • 10,151
  • 6
  • 39
  • 52