String to Long Double using Streams

F

ferran

Hi, I'm trying to convert a string to a long double using streams but
for some reasing seems to give the wrong values, the idea is to do a
precise textual conversion without roundings or truncations, I though
about using strtold but this function is not portable hence that is
why I'm using streams, this is what I'm doing

//-------------------------------------------------------
std::stringstream oStream;
std::string sInput = "3.14159265358979323846";
long double dOutput = 0;

oStream << sInput;
oStream >> dOutput;
//-------------------------------------------------------

This is giving me a result in dOutput of 0.00564616105916946368 !!!!
This is quite strange cos it seems to work perfectly for any numeric
types smaller than 'long double', can anyone tell me what I'm doing
wrong?


Thanks for any offered help
<>Fernando
 
J

John Harrison

ferran said:
Hi, I'm trying to convert a string to a long double using streams but
for some reasing seems to give the wrong values, the idea is to do a
precise textual conversion without roundings or truncations, I though
about using strtold but this function is not portable hence that is

strtold is portable, its a perfectly standard C library function.
why I'm using streams, this is what I'm doing

//-------------------------------------------------------
std::stringstream oStream;
std::string sInput = "3.14159265358979323846";
long double dOutput = 0;

oStream << sInput;
oStream >> dOutput;
//-------------------------------------------------------

This is giving me a result in dOutput of 0.00564616105916946368 !!!!
This is quite strange cos it seems to work perfectly for any numeric
types smaller than 'long double', can anyone tell me what I'm doing
wrong?

I would say, forgetting to rewind the stream. You are writing to the stream
and then trying to read from it when you are still positioned at the end of
the stream.

Try this

std::string sInput = "3.14159265358979323846";
std::istringstream oStream(sInput);
long double dOutput = 0;
oStream >> dOutput;

works for me.

john
 
F

ferran

Hi thanks for your reply, altough I just found out as you said that
actually strtold is portable, for my purposes I still require to solve
this problem using streams, I have tried your solution but yet again I
get a result of 0.00564616105916946368
 
T

Tim Love

Hi thanks for your reply, altough I just found out as you said that
actually strtold is portable, for my purposes I still require to solve
this problem using streams, I have tried your solution but yet again I
get a result of 0.00564616105916946368
I get 3.14159 (aCC on HP-UX) so I'd blame your compiler/libs.
 
F

ferran

Further investigation has come to the conclusion that is the actual
compiler which has a bug!!!
I'm usign Borland C++ Builder, but trying the same code in VC++ and
Dev-C++ I discovered that those two last compilers give the right
value, I just can't believe that!!!
 
J

John Harrison

ferran said:
Further investigation has come to the conclusion that is the actual
compiler which has a bug!!!
I'm usign Borland C++ Builder, but trying the same code in VC++ and
Dev-C++ I discovered that those two last compilers give the right
value, I just can't believe that!!!

Or the library you are working with, I'd try a debugger and find out exactly
what is going wrong.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top