PDA

View Full Version : Evaluating Expression



vajindarladdad
10th March 2009, 07:02
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 .

Boron
11th March 2009, 18:06
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!

wysota
11th March 2009, 18:49
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.

john_god
13th March 2009, 00:08
Check this link

http://www.codeproject.com/KB/recipes/Differentiation.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

lyuts
14th March 2009, 11:42
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...

john_god
14th March 2009, 15:44
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.

lyuts
14th March 2009, 16:34
Yeah, I'm always keeping in mind this Taylor series. I just wanted to point that one should be careful with such calculations.