Post Reply 
prefix notation and () on newRPL project
12-23-2022, 02:59 AM (This post was last modified: 12-25-2022 06:12 PM by Albert Chan.)
Post: #10
RE: prefix notation and () on newRPL project
(12-23-2022 12:02 AM)Albert Chan Wrote:  How to distinguish subtraction from negation
Quote:If you can subtract, do it; if you can’t, then take it as a negation. Subtraction has priority.

Scanning backward, it will handle (op +/-) without issue.

2^-3^4
2^-(3^4)
2^(-(3^4))

2*-3/4
(2*-3)/4
(2*(-3))/4

Patched above (op +/-) logic to calc macro (previous post)
Also, unary +/- precedence placed below POW, above MUL/DIV, similar to Python/Lua

Before:
Code:
        ((+ -)  (if (null? (cdr e))
                    (list (car e) (build a term))  ; unary +/-
                    (list (car e) [rest e] (build a term))))

After:
Code:
        ((+ -)  (if (null? (cdr e))
                    (build [list (car e) a] term)  ; unary +/-
                    (if (memq (op (cdr e)) '(+ - * / ^))
                        (scan (cdr e) [list (car e) a] term)  ; op +/-
                        (list (car e) [rest e] (build a term)))))

Test with calc-expand macro (previous post)

scheme> (calc-expand '(2 ^ - 3 ^ 4)) ; op +/- tests
(^ 2 (- (^ 3 4)))
scheme> (calc-expand '(2 * - 3 / 4))
(/ (* 2 (- 3)) 4)

scheme> (calc-expand '(- 2 ^ 3 ^ 4)) ; unary +/- precedence below POW
(- (^ 2 (^ 3 4)))
scheme> (calc-expand '(- 2 * 3 / 4 )) ; unary +/- precedence above MUL/DIV
(/ (* (- 2) 3) 4)

And, for Mach number test. Spaces are needed to separate the tokens.

scheme> (sqrt (calc 5 *(((((1 + .2 *(350 / 661.5)^ 2)^ 3.5 - 1)* (1 - 6.875E-6 * 25500)^ - 5.2656)+ 1)^ .286 - 1)))
0.8357245351752515


For comparison, we figured precedence rules, then use rpn macro.

scheme> (rpn 350 661.5 / 2 ^ .2 * 1 + 3.5 ^ 1 -
                      1 6.875E-6 25500 * - -5.2656 ^ *
                      1 + .286 ^ 1 - 5 * sqrt @)
0.8357245351752515
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: prefix notation and () on newRPL project - Albert Chan - 12-23-2022 02:59 AM



User(s) browsing this thread: 1 Guest(s)