infix icon indicating copy to clipboard operation
infix copied to clipboard

incorrect evaluation of powers

Open boxxxie opened this issue 7 years ago • 2 comments

2^2^2^2 will give a result that is like (((2 ^ 2) ^ 2) ^ 2) which is 16^2

i actually have my own implementation of what you wrote, that relies on clojure.walk

if you are ok with that, i can issue a PR with some more tests

boxxxie avatar Mar 19 '18 21:03 boxxxie

Is this the infix macro or the from-string (or both?)

would be interested to see the clojure.walk version + how well of a replacement it would be if it is cleaner & provides the same or better functionality

rm-hull avatar Mar 19 '18 22:03 rm-hull

clojure walk version is built off of what you already have, just doesn't have the notion of recursing it's a bit messy for doing the "^" conversion (lots of reversing, but this is code that gets called almost never).

i'll link a gist of the code i use

https://gist.github.com/boxxxie/8a988b4fc5f35d31eb2d4f6f35799463

so, main difference is that i have a mapping of which functions need to be rewritten forward, then backward. i do 2 stages of rewrite, an infix->paren rewrite, then infix->prefix

my functions only deal with binary operators, while your code handles unary. i think it would be easy for me to add that functionality back in (i just had different requirements for the code).

the rewrite-op-backwards rewrite-op-forwards have duplicate code, so they can be refactored

boxxxie avatar Mar 20 '18 02:03 boxxxie