- ABORT
- ABORT"
- ABS
- ACCEPT
- ACTION-OF
- AGAIN
- ALIGN
- ALIGNED
- ALLOT
- AND
- BASE
- BEGIN
- BL
- BUFFER:
- [
- [CHAR]
- [COMPILE]
- [']
- CASE
- C,
- CELL+
- CELLS
- C@
- CHAR
- CHAR+
- CHARS
- COMPILE,
- CONSTANT
- COUNT
- CR
- CREATE
- C!
- :
- :NONAME
- ,
- C"
- DECIMAL
- DEFER
- DEFER@
- DEFER!
- DEPTH
- DO
- DOES>
- DROP
- DUP
- /
- /MOD
- .R
- .(
- ."
- ELSE
- EMIT
- ENDCASE
- ENDOF
- ENVIRONMENT?
- ERASE
- EVALUATE
- EXECUTE
- EXIT
- =
- FALSE
- FILL
- FIND
- FM/MOD
- @
- HERE
- HEX
- HOLD
- HOLDS
- I
- IF
- IMMEDIATE
- INVERT
- IS
- J
- KEY
- LEAVE
- LITERAL
- LOOP
- LSHIFT
- MARKER
- MAX
- MIN
- MOD
- MOVE
- M*
- -
- NEGATE
- NIP
- OF
- OR
- OVER
- 1-
- 1+
- PAD
- PARSE-NAME
- PARSE
- PICK
- POSTPONE
- +
- +LOOP
- +!
- QUIT
- RECURSE
- REFILL
- REPEAT
- RESTORE-INPUT
- R@
- ROLL
- ROT
- RSHIFT
- R>
- SAVE-INPUT
- SIGN
- SM/REM
- SOURCE-ID
- SOURCE
- SPACE
- SPACES
- STATE
- SWAP
- ;
- S\"
- S"
- S>D
- !
- THEN
- TO
- TRUE
- TUCK
- TYPE
- '
- *
- */
- */MOD
- 2DROP
- 2DUP
- 2/
- 2@
- 2OVER
- 2R@
- 2R>
- 2SWAP
- 2!
- 2*
- 2>R
- U.R
- UM/MOD
- UM*
- UNLOOP
- UNTIL
- UNUSED
- U.
- U<
- U>
- VALUE
- VARIABLE
- WHILE
- WITHIN
- WORD
- XOR
- 0=
- 0<
- 0>
- 0<>
- \
- .
- <
- >
- <>
- #>
- <#
- #
- #S
- (
- ?DO
- ?DUP
- >BODY
- >IN
- >NUMBER
- >R

# WITHIN

*n*--

_{1}| u_{1}n_{2}| u_{2}n_{3}| u_{3}*flag*)

Perform a comparison of a test value *n _{1} | u_{1}* with a
lower limit

*n*and an upper limit

_{2}| u_{2}*n*, returning

_{3}| u_{3}*true*if either (

*n*<

_{2}| u_{2}*n*and (

_{3}| u_{3}*n*<=

_{2}| u_{2}*n*and

_{1}| u_{1}*n*<

_{1}| u_{1}*n*)) or (

_{3}| u_{3}*n*>

_{2}| u_{2}*n*and (

_{3}| u_{3}*n*<=

_{2}| u_{2}*n*or

_{1}| u_{1}*n*<

_{1}| u_{1}*n*)) is true, returning

_{3}| u_{3}*false*otherwise. An ambiguous condition exists

*n*,

_{1}| u_{1}*n*, and

_{2}| u_{2}*n*are not all the same type.

_{3}| u_{3}

## See:

## Rationale:

**WITHIN**without mentioning circular number spaces (an undefined term) or providing the code. Here is a number line with the overflow point (

*o*) at the far right and the underflow point (

*u*) at the far left:

*u*---------------

*o*

*n*range straddles the overflow/underflow points or it does not. Lets examine the non-straddle case first:

_{2}| u_{2}... n_{3}| u_{3}*u*-----[.....)-----

*o*

*n*, the ) denotes

_{2}| u_{2}*n*, and the dots and [ are numbers

_{3}| u_{3}**WITHIN**the range.

*n*is greater than

_{3}| u_{3}*n*, so the following tests will determine if

_{2}| u_{2}*n*is

_{1}| u_{1}**WITHIN**

*n*and

_{2}| u_{2}*n*:

_{3}| u_{3}*n*

_{2}| u_{2}*<=*

*n*and

_{1}| u_{1}*n*<

_{1}| u_{1}*n*.

_{3}| u_{3}*u*.....)-----[.....

*o*

*n*is less than

_{3}| u_{3}*n*and the following tests will determine if

_{2}| u_{2}*n*is

_{1}| u_{1}**WITHIN**

*n*and

_{2}| u_{2}*n*:

_{3}| u_{3}*n*

_{2}| u_{2}*<=*

*n*or

_{1}| u_{1}*n*<

_{1}| u_{1}*n*.

_{3}| u_{3}**WITHIN**must work for both signed and unsigned arguments. One obvious implementation does not work: Assume two's-complement arithmetic on a 16-bit machine, and consider the following test:

`33000 32000 34000 WITHIN`

The above implementation returns *false* for that test,
even though the unsigned number 33000 is clearly within the
range {{32000 ... 34000}}.

The problem is that, in the incorrect implementation, the signed comparison < gives the wrong answer when 32000 is compared to 33000, because when those numbers are treated as signed numbers, 33000 is treated as negative 32536, while 32000 remains positive.

Replacing < with U< in the above implementation makes it work with unsigned numbers, but causes problems with certain signed number ranges; in particular, the test:

For two's-complement machines that ignore arithmetic overflow (most machines), the following implementation works in all cases:

## ContributeContributions

## Max sample implementation that can also be interpretedSuggested reference implementation2020-02-05 20:49:51

The current sample implementation uses `>R`

and `R>`

, which are compile-only (technically "Interpretation semantics for these words are undefined").

Instead `WITHIN`

is expected to work also when interpreting.

The following implementation fixes that, and is correct under the same condition "For two's-complement machines that ignore arithmetic overflow" as the current sample implementation:

`: WITHIN ( test low high -- flag ) OVER - ROT ROT - U> ;`