Path: eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: anton@mips.complang.tuwien.ac.at (Anton Ertl) Newsgroups: comp.lang.forth Subject: Re: WITHIN Date: Sat, 08 Mar 2014 15:43:45 GMT Organization: Institut fuer Computersprachen, Technische Universitaet Wien Lines: 84 Message-ID: <2014Mar8.164345@mips.complang.tuwien.ac.at> References: <52e54d17.461102185@news.demon.co.uk> <7xr46lruq1.fsf@ruckus.brouhaha.com> <7x7g8cvagd.fsf@ruckus.brouhaha.com> <2014Mar6.190908@mips.complang.tuwien.ac.at> <2014Mar7.135132@mips.complang.tuwien.ac.at> <7x7g85him2.fsf@ruckus.brouhaha.com> Injection-Info: mx05.eternal-september.org; posting-host="d47d3421039fe8026514328ad0ebacae"; logging-data="5500"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18kYLQHKAUs1enL7KrXm2is" X-newsreader: xrn 10.00-beta-3 Cancel-Lock: sha1:By3JHW6ATW2yQdZK38495AXO1Hk= mhx@iae.nl writes: >On Friday, March 7, 2014 9:13:57 PM UTC+1, Paul Rubin wrote: >> anton@mips.complang.tuwien.ac.at (Anton Ertl) writes: >[..] > >Your Forth is improving. Try this :-) > >-marcel > >--- ------------------------------------------------------------------ >ANEW -within > >: endpoints ( n a b -- a<=n n: outside ( n a b -- flag ) endpoints or ; \ precondition: a > b >: inside ( n a b -- flag ) endpoints and ; \ precondition: a <= b >: within1 ( n a b -- flag ) 2dup <= if inside else outside then ; > >: within2 ( n a b -- flag ) > params| n a b | > a b <= if a n <= n b < and > else a n <= n b < or > then ; > >: within3 ( n a b -- flag ) params| n a b | a n <= n b < a b <= if and else or then ; >: within4 ( n a b -- flag ) params| n a b | a n <= n b < 2dup and -rot or a b <= if drop else nip then ; > >: within5 ( n a b -- flag ) params| n a b | a b > >R > a n <= n b < and a n <= n b < or and > a n <= n b < and a n <= n b < or xor > R> and or ; > >DOC >(* > x = a&b y = a|b > x y c=1 -> x > x y c=0 -> y > > x y c out > ========= > 0 0 0 0 > 0 0 1 0 > 0 1 0 1 > 0 1 1 0 ~c & xor(x,y) = a b and a b or xor c invert and > 1 0 0 1 > 1 0 1 0 > 1 1 0 1 \ x&y => a b and a b or and > 1 1 1 1 / > >*) >ENDDOC > >: within6 ( n a b -- flag ) 2DUP > >R ( n a b ) ROT TUCK ( a n b n ) >= >R <= >S > S R@ and S R@ or and > S R@ and S> R> or xor R> and or ; You could also try : within7 { n1 n2 n3 -- f } \ direct from the spec n2 n3 < n2 n1 <= n1 n3 < and and n2 n3 > n2 n1 <= n1 n3 < or and or ; : within8 { n a b -- f } \ ANSI C-ish: does not use overflow, but uses arithmetic on flags. a b > a n <= n b < + + -2 = ; : within9 { n a b -- f } \ proper WITHIN, implemented with locals n a - b a - u< ; : withina ( n a b -- f ) \ purist's proper WITHIN over - >r - r> u< ; The latter two are to check whether your WITHIN does better than one written in high-level, and how much of a slowdown locals produce on the measured Forth system (ideally none). - anton -- M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html New standard: http://www.forth200x.org/forth200x.html EuroForth 2013: http://www.euroforth.org/ef13/