 |
|
 |
This is very nice work. I tried out the test page and check out the source. It will be nice to include add "&&" and "||" functions.
Expression can be (3+x)>0 && (k<5), ((x+y)>0 || ((x-7)>y))
Thanks!
|
|
|
|
 |
|
 |
Downloaded and tried the evaluation code and the very first attempt produced an error with no output. I input what I believe is a valid expression but it does not like it and does not report an error, it just does nothing. I looked through the code, tried the Expression tester, then the Tokenizer (which did work) then the Token Tester which didn't. I tracked it down to the fact that the InFixToPostFix function in Evaluator.js fails at the 'case "(" :' section on the 'if (IsFunction(myStack.Get(0)))' test, specifically the 'myStack.Get(0)' which returns 'undefined'. This is because the expression I tried started with a left bracket, i.e. "(". The whole expression was
(2^4+6)/3
The brackets were there to force evaluation of 2^4+6 before division by 3, which without the brackets would be evaluated as 2^4, then 6/3 and the two results added to give = 18 when it should be 2^4+6 then that sum of 22 divided by three. With brackets it should be translated to 2,4,^,6,+,3,/ while without it is 2,4,^,6,3/+.
Is this a bug (that does not allow an open bracket as the first infix item) or is my expression with the brackets an invalid mathematical expression?
|
|
|
|
 |
|
 |
Hello,
I have corrected the code and posted it back. The article and the code should get updated soon.
Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.
|
|
|
|
 |
|
 |
Your page http://www.codeproject.com/KB/scripting/jsexpressioneval.aspx[^] gives a cut down history of RPN (Reverse Polish Notation) and its first implementation. The text below describes in greater detail the development and first use of RPN in computers at http://www.calculator.org/RPN.html[^]. As you can see, it was first implemented in the English Electric computers of the early 1960s. This is supported by the document describing the KDF9 Algol Translator at http://www.cs.ncl.ac.uk/research/pubs/books/papers/124.pdf[^]. However the description of the first 'user interface' to use RPN is not contested. I just feel a fuller 'overview' is more representative.
Polish Notation was invented in the 1920's by Polish mathematician Jan Lukasiewicz, who showed that by writing operators in front of their operands, instead of between them, brackets were made unnecessary. Although Polish Notation was developed for use in the fairly esoteric field of symbolic logic, Lukasiewicz noted that it could also be applied to arithmetic. In the late 1950's the Australian philosopher and early computer scientist Charles L. Hamblin proposed a scheme in which the operators follow the operands (postfix operators), resulting in the Reverse Polish Notation. This has the advantage that the operators appear in the order required for computation. RPN was first used in the instruction language used by English Electric computers of the early 1960's. Engineers at the Hewlett-Packard company realised that RPN could be used to simplify the electronics of their calculators at the expense of a little learning by the user. The first "calculator" to use RPN was the HP9100A, which was introduced in 1968, although this machine is now regarded by many as the first desktop computer.
Sorry to interject a difference of detail not connected directly with the code described, but we can't have our cousins across the pond taking all the credit!
|
|
|
|
 |
|
 |
Hello,
Thank's a lot for bringing it to my notice. I totally agree with your statement that "we can't have our cousins across the pond taking all the credit!". I will try to put up a link and correct the misleading paragraph.
Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.
|
|
|
|
 |
|
 |
hi thanks for your great work.
I tried to use IIF function but it seems i'm doing it wrong.
i tried it as: IIF(1,2,1>2) but it couldn't be evalueted.
please tell me how can i use IIF.
|
|
|
|
 |
|
 |
Hello,
The IIF statement should be in this form IIF( <condition>, <true result>, <false result> ). So your statement will become IIF(1>2,1,2).
Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.
|
|
|
|
 |
|
 |
if i'll give iif(1>2,1,2) like this,it does nothing
|
|
|
|
 |
|
 |
Fine component . Will it be possible to use variables instead of constant?
|
|
|
|
 |
|
 |
Hello,
This support is already available. You will have to call the AddVariable method of Expression. This method takes two parameters. The variable name as used in the expression and it's value. Refer to ExpressionTester.html page for more details.
Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.
|
|
|
|
 |
|
 |
Thanks a lot for the nice article. Although I am not a javascript guy but a nice explaination of infix notation in your article helped my solving a bug in my own app that I wrote for a unit converter application
Grewal.
http://www.mycoolaids.com
|
|
|
|
 |
|
 |
I have found another bug:
in Evaluator.js Line 766
'varTmp' should be 'varTerm'
if (varTmp == "LEFT")
pStack.Push(varTerm.substring(0, objTmp)); //changed
else
pStack.Push(varTerm.substr((varTerm.length - objTmp), objTmp)); //changed
break;
Jay
|
|
|
|
 |
|
 |
If have found a Bug:
if you try to eval (1+1)/2 there is an exception in line 403 in Tokanizer.js.
if you change it to
function IsFunction(strArg)
{
var idx = 0;
if(strArg == null) //Added
return false; //Added
strArg = strArg.toUpperCase();
for (idx = 0; idx < lstFuncOps.length; idx++)
{
if (strArg == lstFuncOps[idx])
return true;
}
return false;
}
the code seams to work. please verify the code
Jay
-- modified at 1:54 Tuesday 26th September, 2006
|
|
|
|
 |
|
 |
Raising error, when the first char is '(' will call IsFunction to check. In order not to modify the author's thinking. Insert a try & catch section to prevent the first '(' appears at the beginning string.
function IsFunction(strArg)
{
var idx = 0;
try{
strArg = strArg.toUpperCase();
for (idx = 0; idx {
if (strArg == lstFuncOps[idx])
return true;
}
return false;
}catch(E){
return false;
}
}
|
|
|
|
 |
|
 |
Dear Prasad Khandekar,
I tried the following Infix expression using JS Expression Evaluator, but in vain, I am only getting an error.
Infix - a & b | c | d | e | f | g
I am able to get posfix equivalent for the above.
Postfix - {a}{b}{&}{c}{|}{d}{|}{e}{|}{f}{|}{g}{|}
However, I unable to get the results for evaluating the postfix expression.
Please help.
With thanks,
Gopalakrishnan,B.R.
|
|
|
|
 |
|
 |
Hello,
I have updated the sample and the javascript to demonstrate the use of variables. Since this is an edited article the update should be available in next 2/3 days. Keep watching.
Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.
|
|
|
|
 |
|
 |
SQRT function does not work because there is a small bug in the file evaluator.js. letter T shall be add to 'SQR' at line 460.
It don't mean a thing if it ain't got that swing !
|
|
|
|
 |
|
 |
Hello Raphael,
Thank's a lot for pointing out the bug. I have updated the article's content, the associated souce code and submitted to the editors. The new contents should be available very soon.
Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.
|
|
|
|
 |
|
 |
Javascript supports an eval() function for evaluating expressions at runtime.
eval("1+2") == 3
you can even access local and global variables.
Why did you choose to implement your own mechanism for that?
Jaroslaw Kowalski
http://blog.jkowalski.net/
|
|
|
|
 |
|
 |
For the fun of it ?
According to some security rules, the eval() function is sometimes disabled to prevent malware to perform on your computer. Thus, this 'mechanism' offer the liberty to do some math evaluation without 'eval()'
Kochise
In Code we trust !
|
|
|
|
 |
|
 |
Hi,
I know that javascript does have have a Eval function. The main reason to write this was to understand the expression evaluator mechanism in detail and the reason I chose javascript is because it is very easy to implement this mechanism and test it.
Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.
|
|
|
|
 |