Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WITHIN

3 views
Skip to first unread message

MarkWills

unread,
Nov 27, 2009, 3:00:38 AM11/27/09
to
In the below, 0=false; -1=true

6 7 10 WITHIN . 0 ok (too low)
11 7 10 WITHIN . 0 ok (too high)
8 7 10 WITHIN . -1 ok
9 7 10 WITHIN . -1 ok
7 7 10 WITHIN . -1 ok <----- correct?
10 7 10 WITHIN . -1 ok <----- correct?

In other words, what is the behaviour for the edge cases, 7 & 10?

For my purposes, WITHIN is:

WITHIN ( n low_limit high_limit -- true/false)

Furthermore, I presume WITHIN makes signed comparisons?

Thanks

Mark

Celime

unread,
Nov 27, 2009, 4:40:50 AM11/27/09
to
Hello Mark,

In SwiftForth:

7 7 10 within . -1 ok
10 7 10 within . 0 ok

Strange, isn't it ?

Charles

"MarkWills" <markrob...@yahoo.co.uk> a �crit dans le message de
news:
cbc6e50c-684b-47e7...@o10g2000yqa.googlegroups.com...

Andrew Haley

unread,
Nov 27, 2009, 5:02:46 AM11/27/09
to
MarkWills <markrob...@yahoo.co.uk> wrote:
> In the below, 0=false; -1=true

> 6 7 10 WITHIN . 0 ok (too low)
> 11 7 10 WITHIN . 0 ok (too high)
> 8 7 10 WITHIN . -1 ok
> 9 7 10 WITHIN . -1 ok
> 7 7 10 WITHIN . -1 ok <----- correct?

Yes.

> 10 7 10 WITHIN . -1 ok <----- correct?

No.

> In other words, what is the behaviour for the edge cases, 7 & 10?

On a 2's complement system,

: within ( n1|u1 n2|u2 n3|u3 -- flag ) over - >r - r> u< ;

If your version does not give the same results as this it's wrong.

> For my purposes, WITHIN is:

> WITHIN ( n low_limit high_limit -- true/false)

> Furthermore, I presume WITHIN makes signed comparisons?

No, as you see from the definition above. Please refer to the
standard for more details.

Andrew.

Josh Grams

unread,
Nov 27, 2009, 7:51:39 AM11/27/09
to
Celime wrote: <4b0f9d48$0$398$5f6a...@news.scarlet.nl>

> In SwiftForth:
>
> 7 7 10 within . -1 ok
> 10 7 10 within . 0 ok
>
> Strange, isn't it ?

If you want to find whether an address is in a block of memory
(addr u), you do `OVER + SWAP ( BOUNDS ) WITHIN`. The lower limit
is usable, but the upper limit is off the end of the block.

You may, of course, also want a word that includes both limits in
the range: I think the common name for this is BETWEEN...

--Josh

MarkWills

unread,
Nov 27, 2009, 8:04:42 AM11/27/09
to
On Nov 27, 11:02 am, Andrew Haley <andre...@littlepinkcloud.invalid>
wrote:

Thank you Andrew. Your version worked perfectly on my implimentation.
Good news!

Regards

Mark

Celime

unread,
Nov 27, 2009, 9:55:13 AM11/27/09
to
Hello Josh,

More generally, it's probably because arrays are 0-based: the first
index is 0, not 1. Array upper index = count-1.

In natural (and math) language, the definition of WITHIN is different.

Thanks,
Charles


"Josh Grams" <jo...@qualdan.com> a �crit dans le message de news:
vXPPm.27117$kY2....@newsfe01.iad...

Krishna Myneni

unread,
Nov 27, 2009, 11:12:58 AM11/27/09
to


Below is test code for WITHIN, which I incorporate into the regression
tests for kForth. The tests rely on ttester.x, an improved/extended
version of the Hayes test driver.

testing WITHIN

t{ 0 0 0 within -> false }t
t{ 2 6 5 within -> true }t
t{ 2 6 2 within -> false }t
t{ 2 6 6 within -> false }t
t{ -6 -2 -4 within -> true }t
t{ -2 -6 -4 within -> false }t
t{ -6 -2 -2 within -> false }t
t{ -6 -2 -6 within -> false }t
t{ -1 2 1 within -> true }t
t{ -1 2 2 within -> false }t
t{ -1 2 -1 within -> false }t
t{ 0 -1 1 within -> true }t

Jonah Thomas

unread,
Nov 27, 2009, 11:59:56 AM11/27/09
to
"Celime" <cm17...@scarlet.be> wrote:

> In SwiftForth:
>
> 7 7 10 within . -1 ok
> 10 7 10 within . 0 ok
>
> Strange, isn't it ?

With 2's-complement, this gives you the same result whether your numbers
are signed or unsigned.

Usually, if you want the right-hand bound to test true you can get that
result with 1+ .
So it's simple and elegant, very low overhead, provided you're willing
to learn how to use it.

Elizabeth D Rather

unread,
Nov 27, 2009, 1:31:53 PM11/27/09
to
Andrew Haley wrote:
> MarkWills <markrob...@yahoo.co.uk> wrote:
>> In the below, 0=false; -1=true
>
>> 6 7 10 WITHIN . 0 ok (too low)
>> 11 7 10 WITHIN . 0 ok (too high)
>> 8 7 10 WITHIN . -1 ok
>> 9 7 10 WITHIN . -1 ok
>> 7 7 10 WITHIN . -1 ok <----- correct?
>
> Yes.
>
>> 10 7 10 WITHIN . -1 ok <----- correct?
>
> No.

In other words, WITHIN is inclusive at the lower end of its range, and
exclusive at the top. This is consistent with the behavior of DO...LOOP

: TRY ( -- ) 10 0 DO I . LOOP ;

...prints 0 thru 9. That's a good way to remember it!

As others have noted, BETWEEN is a common-usage (though not Standard)
word that's inclusive at the top end as well.

I don't care for using 1+ to adjust the limit. Better to set the limit
one higher if you need to.

Cheers,
Elizabeth

--
==================================================
Elizabeth D. Rather (US & Canada) 800-55-FORTH
FORTH Inc. +1 310.999.6784
5959 West Century Blvd. Suite 700
Los Angeles, CA 90045
http://www.forth.com

"Forth-based products and Services for real-time
applications since 1973."
==================================================

MarkWills

unread,
Nov 28, 2009, 5:07:58 AM11/28/09
to
On Nov 27, 7:31 pm, Elizabeth D Rather <erat...@forth.com> wrote:
> Andrew Haley wrote:
> Los Angeles, CA 90045http://www.forth.com

>
> "Forth-based products and Services for real-time
> applications since 1973."
> ==================================================

Hi Elizabeth,

Yes, confirmed: My DO ... LOOP works as expected. Phew!

(Between is a good one, though I'll just define it as a word when
needed, rather than code it into ROM as a primitive)

Thanks

Mark

Ed

unread,
Nov 30, 2009, 11:18:46 AM11/30/09
to
MarkWills wrote:
> On Nov 27, 7:31 pm, Elizabeth D Rather <erat...@forth.com> wrote:
> > Andrew Haley wrote:
> > > MarkWills <markrobertwi...@yahoo.co.uk> wrote:
> > >> In the below, 0=false; -1=true
> >
> > >> 6 7 10 WITHIN . 0 ok (too low)
> > >> 11 7 10 WITHIN . 0 ok (too high)
> > >> 8 7 10 WITHIN . -1 ok
> > >> 9 7 10 WITHIN . -1 ok
> > >> 7 7 10 WITHIN . -1 ok <----- correct?
> >
> > > Yes.
> >
> > >> 10 7 10 WITHIN . -1 ok <----- correct?
> >
> > > No.
> >
> > In other words, WITHIN is inclusive at the lower end of its range, and
> > exclusive at the top. This is consistent with the behavior of DO...LOOP
> >
> > : TRY ( -- ) 10 0 DO I . LOOP ;
> >
> > ...prints 0 thru 9. That's a good way to remember it!
> >
> > As others have noted, BETWEEN is a common-usage (though not Standard)
> > word that's inclusive at the top end as well.
> >
> > I don't care for using 1+ to adjust the limit. Better to set the limit
> > one higher if you need to.

While that's more efficient, it's also confusing. E.g. it's
not obvious what is the range being tested in these cases:

CHAR A CHAR G WITHIN
CHAR 0 CHAR : WITHIN
670 856 WITHIN

Given the drawbacks of using WITHIN to simulate
BETWEEN, it's less hassle to just have both functions.

A version of BETWEEN that works equally with signed
or unsigned values can be found here:
http://dxforth.webhop.org/between.html

Jerry Avins

unread,
Nov 30, 2009, 2:15:56 PM11/30/09
to
Elizabeth D Rather wrote:
> Andrew Haley wrote:
>> MarkWills <markrob...@yahoo.co.uk> wrote:
>>> In the below, 0=false; -1=true
>>
>>> 6 7 10 WITHIN . 0 ok (too low)
>>> 11 7 10 WITHIN . 0 ok (too high)
>>> 8 7 10 WITHIN . -1 ok
>>> 9 7 10 WITHIN . -1 ok
>>> 7 7 10 WITHIN . -1 ok <----- correct?
>>
>> Yes.
>>
>>> 10 7 10 WITHIN . -1 ok <----- correct?
>>
>> No.
>
> In other words, WITHIN is inclusive at the lower end of its range, and
> exclusive at the top. This is consistent with the behavior of DO...LOOP
>
> : TRY ( -- ) 10 0 DO I . LOOP ;
>
> ...prints 0 thru 9. That's a good way to remember it!
>
> As others have noted, BETWEEN is a common-usage (though not Standard)
> word that's inclusive at the top end as well.
>
> I don't care for using 1+ to adjust the limit. Better to set the limit
> one higher if you need to.

BETWEEN is the way it is because computers work the way they work. It is
one og the most elegant bits of assembly code I know.

Jerry
--
Engineering is the art of making what you want from things you can get.
�����������������������������������������������������������������������

0 new messages