473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

String to Long Double using Streams

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
Jul 22 '05 #1
5 3594

"ferran" <fe*****@yahoo.com> wrote in message
news:53**************************@posting.google.c 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
Jul 22 '05 #2
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" <jo*************@hotmail.com> wrote in message news:<2p************@uni-berlin.de>...
"ferran" <fe*****@yahoo.com> wrote in message
news:53**************************@posting.google.c 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

Jul 22 '05 #3
fe*****@yahoo.com (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.
Jul 22 '05 #4
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" <jo*************@hotmail.com> wrote in message news:<2p************@uni-berlin.de>...
"ferran" <fe*****@yahoo.com> wrote in message
news:53**************************@posting.google.c 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

Jul 22 '05 #5

"ferran" <fe*****@yahoo.com> wrote in message
news:53**************************@posting.google.c 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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: rainmaker1234 | last post by:
Its very simple in VC++. In the followeing code I have declared a String, and a double than I am taking the string and converting it into Double. getch() at the end is only to pause the screen so...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
20
by: Trond Valen | last post by:
Hi! Stupid atof, it returns 0.0 when it tries to parse something like "fish". So I don't know whether the number was really 0 or a string that couldn't be parsed. Is there a better way to do...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
7
by: Eric Lilja | last post by:
Hello, I have an unsigned long that I need to convert to a std::string. The unsigned long holds 32-bit checksums and sometimes the most significant byte is 0 and in those cases the string should be...
15
by: shiniskumar | last post by:
How to convert string to double? Ive got a double variable dTot; its value is 5.037717235E7 when i did FreemarkerTools.formatDecimal(dTot) i got it as string "50377172.35". now i want to...
14
by: Aman JIANG | last post by:
hi i need a fast way to do lots of conversion that between string and numerical value(integer, float, double...), and boost::lexical_cast is useless, because it runs for a long time, (about 60...
23
by: KIRAN | last post by:
Hi all, can i split a C string like this? char * p = "Hello \ World\n\r"; is this according to standard? Any help or link to standard regarding the above doubt is aprreciated... Regards, Kiran
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.