boussaffawalid / muparserx

Automatically exported from code.google.com/p/muparserx
Other
0 stars 0 forks source link

DblValReader::IsValue broken for numbers at the end of the string #2

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

mup::ParserX p(mup::pckALL_NON_COMPLEX);
p.SetExpr("0.0");
p.Eval();

The Eval() throws an exception that the period "." is not a valid token.  

I tracked it down to the code in DbpValReader::IsValue.  Specifically, the 
existing code is

  bool DblValReader::IsValue(const char_type *a_szExpr, int &a_iPos, Value &a_Val)
  {
    stringstream_type stream(a_szExpr + a_iPos);
    float_type fVal(0);
    std::streamoff iEnd(0);

    stream >> fVal;
    iEnd = stream.tellg();   // Position after reading

    if (iEnd==-1)
      return false;

    a_iPos += (int)iEnd;

If the stream reads to the end of the string, then tellg will return -1 
regardless of whether the value was correctly interpreted.  The better way is 
to check the failbit explicitly.

  bool DblValReader::IsValue(const char_type *a_szExpr, int &a_iPos, Value &a_Val)
  {
    stringstream_type stream(a_szExpr + a_iPos);
    float_type fVal(0);

    stream >> fVal;
    if(stream.fail())
      return false;

    if(stream.eof())
      {
        a_Val=cmplx_type(fVal,0.0);
        a_iPos=strlen(a_szExpr);
        return true;
      }

    std::streamoff iEnd(stream.tellg());   // Position after reading

    a_iPos += (int)iEnd;

What version of the product are you using? On what operating system?

muparserx v2_0_1, though the code seems to be the same in the repository.
Debian testing
g++ 4.6

Original issue reported on code.google.com by wlandry@caltech.edu on 8 Nov 2011 at 10:42

GoogleCodeExporter commented 9 years ago
Thanks for the report, sounds somewhat familiar. Here is a lengthy discussion 
of the details:

http://sourceforge.net/tracker/?func=detail&aid=3403905&group_id=137191&atid=737
979

I thought this was already fixed but it seems the fix got lost when moving the 
project to google code. I updated the SVN repository it should work now.

Original comment by ib...@gmx.info on 8 Nov 2011 at 11:13

GoogleCodeExporter commented 9 years ago

Original comment by ib...@gmx.info on 10 Nov 2011 at 10:12