3 ms·
> The correct solution in standard Forth is "DUP >R DUP * SWAP * + R> ABS -" I don’t think that can be correct. Multiplication is commutative, so SWAP before *
by Someone 9y ago
> The correct solution in standard Forth is "DUP >R DUP * SWAP * + R> ABS -"
I don’t think that can be correct. Multiplication is commutative, so SWAP before * doesn’t do anything, getting “DUP >R DUP * * + R> ABS -“, which means it computes x^2 y for some x and y somewhere along the way.
To compute y^2+x^2-|y| in forth, I would use multiple functions, for example
: ^2 DUP * ;
: x^2-|x| DUP ^2 SWAP ABS - ;
: f x^2-|x| SWAP ^2 + ;
- astrobe_ 9y agoI stand corrected, I forgot a DUP: DUP >R DUP * SWAP DUP * + R> ABS - I didn't want to use helper words to allow for a 1:1 comparison. And I also forgot that standard Forth has TUCK (<=> SWAP OVER), so one can do it without the return stack as well (using helpers this time): TUCK ^2 SWAP ^2 + SWAP ABS -