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
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...
> 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.
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
Thank you Andrew. Your version worked perfectly on my implimentation.
Good news!
Regards
Mark
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...
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
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.
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."
==================================================
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
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
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.
�����������������������������������������������������������������������