Results 1 to 6 of 6

Thread: Convert "(scaleValue<0.5)?(4.5*data1):(1.5*data2)" to double value

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2011
    Posts
    36
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Convert "(scaleValue<0.5)?(4.5*data1):(1.5*data2)" to double value

    Hello,

    I would like to convert "(scaleValue<0.5)?(4.5*data1) : (1.5*data2)" to double value with following assumptions,

    scaleValue = 1.5;
    data1 = 2.0;
    data2 = 4.0;

    I would like to convert the above string and return result as double value. How to do this?

    Regards,
    Sankar.

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Convert "(scaleValue<0.5)?(4.5*data1):(1.5*data2)" to double value

    There is no string in any of the expressions above. Do your assignments in the branch statements.

  3. #3
    Join Date
    Feb 2011
    Posts
    36
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Convert "(scaleValue<0.5)?(4.5*data1):(1.5*data2)" to double value

    I mean the expression is stored in a string as given below,
    QString strData = "(scaleValue<0.5)?(4.5*data1) : (1.5*data2)";

    I would like to evaluate the expression and returns its resultant value.


    Regards,
    Sankar.

  4. #4
    Join Date
    Jan 2011
    Posts
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Convert "(scaleValue<0.5)?(4.5*data1):(1.5*data2)" to double value

    You already named it - you need an expression evaluator :-)
    Search the internet for Scanner, Parser, expression evaluation. Depending on the functionality you need, this can become quite complex - http://www.arstdesign.com/articles/e...valuation.html gives you an idea. For sure there are expression evaluators in C/C++ available on the internet which you can start with and adjust to your needs.

    Regards,
    Andreas

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Convert "(scaleValue<0.5)?(4.5*data1):(1.5*data2)" to double value

    Qt Code:
    1. QString strData = "(scaleValue<0.5)?(4.5*data1) : (1.5*data2)";
    2. QScriptEngine engine;
    3. QScriptValue result = engine.evaluate(strData);
    4. qreal resultDouble = result.toNumber(); // or qscriptvalue_cast<qreal>(result);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Feb 2011
    Posts
    36
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Convert "(scaleValue<0.5)?(4.5*data1):(1.5*data2)" to double value

    Thanks Wysota.

    Will try to make use of this.

    Regards,
    Sankar.

Similar Threads

  1. Replies: 1
    Last Post: 3rd May 2010, 09:25
  2. Replies: 1
    Last Post: 7th April 2010, 21:46
  3. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  4. qFromLittleEndian returning "double"
    By gyre in forum Newbie
    Replies: 4
    Last Post: 30th December 2007, 01:10
  5. Convert Windows "HWND" to QWidget* pointer?
    By gerome69 in forum Qt Programming
    Replies: 4
    Last Post: 20th September 2006, 13:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.