String to Long Double using Streams

Discussion in 'C++' started by ferran, Sep 2, 2004.

  1. ferran

    ferran Guest

    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
     
    ferran, Sep 2, 2004
    #1
    1. Advertising

  2. "ferran" <> wrote in message
    news:...
    > 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
     
    John Harrison, Sep 2, 2004
    #2
    1. Advertising

  3. ferran

    ferran Guest

    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" <> wrote in message news:<>...
    > "ferran" <> wrote in message
    > news:...
    > > 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
     
    ferran, Sep 3, 2004
    #3
  4. ferran

    Tim Love Guest

    (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.
     
    Tim Love, Sep 3, 2004
    #4
  5. ferran

    ferran Guest

    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" <> wrote in message news:<>...
    > "ferran" <> wrote in message
    > news:...
    > > 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
     
    ferran, Sep 3, 2004
    #5
  6. "ferran" <> wrote in message
    news:...
    > 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
     
    John Harrison, Sep 3, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ferran
    Replies:
    9
    Views:
    3,116
    Kevin Goodsell
    Apr 12, 2004
  2. JKop
    Replies:
    4
    Views:
    16,570
  3. Sydex
    Replies:
    12
    Views:
    6,646
    Victor Bazarov
    Feb 17, 2005
  4. Michael Mair

    Typecast long double->double seems to go wrong

    Michael Mair, May 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    694
    Michael Mair
    May 24, 2004
  5. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,246
    Peter Shaggy Haywood
    Sep 20, 2005
Loading...

Share This Page