Thanks for your praises :-)

Your implementation of an arithmetic calculator is based on
convenience only. Two illustrations:

(1 + 2) * (5 - 3) -> parenthesis won't change the order of evaluation,
and you have to store the result of (1 + 2) in the heap to do the
computation (in reverse polish notation : 1 2 + 5 3 - *)

2 * (30 - 5) -> the order of evaluation *can* be left-to-right : in
RPN : 2 30 5 - *. You were evaluating the parenthesis first, but it
wasn't necessarily the easiest solution. As long as you are doing the
right associations, the order doesn't matter (with regards to integers
and multiplication/addition, of course). You could still compute "2",
then compute "30 - 5", then compute "#1 * #2". Since multiplication is
commutative, you can alse compute "30 - 5" then "2". But you can never
compute "2 * 30" then "5". The order of evaluation can be changed
(with * in arithmetics, not with || in lazy-evaluation languages), but
not the precedence of operators: a definitive proof that the two are
(mostly) unrelated.

Regards.


2011/11/7 HankyPanky <[email protected]>:
> Well, sounds like I mistakenly thanked another fella for Cédric.
> Anyway, nothing changes my intention, you guys spend some time on
> someone's question and that's for sure appreciated.
>
> And you Cédric are not an exception especially with your fast and
> thorough reply. One last word with you, when I used to attend the
> college I remember that I wrote an expression parser for evaluating
> simple mathematical expressions such as 2 + 3 * 4 - 5 etc.
>
> Back then, parenthesis not only changed the precedence but also the
> order of evaluation. I mean writing an expression like 2 * (30 - 5)
> forced my implementation (stack-based) to first evaluate 30 - 5 to 25
> followed by a multiplication which yielded 50. On the other hand,
> without parentheses first 2 * 30 got evaluated to 60 followed by a
> subtraction which in turn yielded 55. Can you put all these together
> and make it even clearer for me? I mean, does this challenge your last
> sentence which goes: Simply put, precedence has nothing to do with
> order of evaluation?
>
> --
> To view archived discussions from the original JSMentors Mailman list: 
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here: 
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
>

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to