Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > String to Long Double using Streams

Reply
Thread Tools

String to Long Double using Streams

 
 
ferran
Guest
Posts: n/a
 
      09-02-2004
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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-02-2004

"ferran" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
 
ferran
Guest
Posts: n/a
 
      09-03-2004
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



"John Harrison" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> "ferran" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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

 
Reply With Quote
 
Tim Love
Guest
Posts: n/a
 
      09-03-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (ferran) writes:

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

I get 3.14159 (aCC on HP-UX) so I'd blame your compiler/libs.
 
Reply With Quote
 
ferran
Guest
Posts: n/a
 
      09-03-2004
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!!!

"John Harrison" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> "ferran" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      09-03-2004

"ferran" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
float, double, long double JKop C++ 4 08-08-2004 07:12 PM
Cast from (long double*) to (const double*) ferran C++ 9 04-12-2004 05:05 PM



Advertisments