Results 1 to 7 of 7

Thread: Evaluating Expression

  1. #1
    Join Date
    Dec 2008
    Location
    PUNE (INDIA)
    Posts
    49
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Evaluating Expression

    Hello All Experts ,
    I want to evaluate expressions like these

    1) 12+13*2^3+sin90
    2) 87*6+(5+7)(3*2) *tan(30)

    But i dont want to use Math.sin() , Math.pow() etc functions .
    Can anybody tell me which class to use .
    I had used QScriptEngine , QScriptValue classes .
    Thanks & Regards ,

    Vajindar Laddad .
    Trainee Developer.
    (INDIA).
    91+9325014248

  2. #2
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2
    Thanked 29 Times in 28 Posts

    Default Re: Evaluating Expression

    pow() can easily be replaced with a loop multiplying the basis as often as the exponent claims. No big deal .

    If you don't want to use the "built-in" trigonometric functions of C/C++ you probably find an alternative library in the internet.
    Or you implement trigonometric functions yourself. There are surely some algorithms in the net which also have acceptable performance.
    But you really should use standard library functions. That's why they exist: for all the developers who need them!

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

    Default Re: Evaluating Expression

    Do you want to evaluate these in the script environment or somewhere in general? If the former and you don't want to use Math namespace, you have to provide your own versions of the functions. The problem will be only with "^" as you have to replace it with a call to proper function.
    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.


  4. #4
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60
    Thanks
    23
    Thanked 32 Times in 28 Posts

    Default Re: Evaluating Expression

    Check this link

    http://www.codeproject.com/KB/recipe...entiation.aspx


    You could also write your own parser, operator ^ can be implemented with friend operator; sin, cos, etc, can be implement with the taylor series

  5. The following user says thank you to john_god for this useful post:

    vajindarladdad (13th March 2009)

  6. #5
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 29 Times in 27 Posts

    Default Re: Evaluating Expression

    Definitely you have to use calculus of approximations...


    Boron, i don't think that replacing a pow() function with a loop is "no big deal".
    Here is an exmaple, 2.45^1.7355731...
    I'm a rebel in the S.D.G.

  7. #6
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60
    Thanks
    23
    Thanked 32 Times in 28 Posts

    Default Re: Evaluating Expression

    Quote Originally Posted by lyuts View Post
    Here is an exmaple, 2.45^1.7355731...
    This example is a bit tricky but still can be done with some workaround.

    You have to use Taylor series where : a^x = 1 + x*ln(a) + (x*ln(a))^2/factorial(2) + (x*ln(a))^3/factorial(3) + ...

    It's possible to define a precision value (like 0.00001) and make a loop that stop after the precision we want has beem achieved.

    But I think it's preferrible to use the existent functions because they should optimized.

  8. #7
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 29 Times in 27 Posts

    Default Re: Evaluating Expression

    Yeah, I'm always keeping in mind this Taylor series. I just wanted to point that one should be careful with such calculations.
    I'm a rebel in the S.D.G.

Similar Threads

  1. Regular Expression for QDate [YYYY/MM/DD] format
    By bera82 in forum Qt Programming
    Replies: 6
    Last Post: 3rd August 2019, 10:40
  2. Regular Expression Problem
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2009, 10:41
  3. set a regular expression on QTextEdit
    By mattia in forum Newbie
    Replies: 3
    Last Post: 27th March 2008, 11:16
  4. Find with Regular Expression?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2007, 15:44
  5. reg expression
    By mickey in forum General Programming
    Replies: 8
    Last Post: 5th July 2007, 21:21

Tags for this Thread

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.