gavino <gavcomedy@[EMAIL PROTECTED]
> wrote:
> from http://www.forth.com/starting-forth/sf4/sf4.html
>
> Here's a more useful example. You know that dividing any number by
> zero is impossible, so if you try it on a computer, you'll get an
> incorrect answer. We might define a word which only performs division
> if the denominator is not zero. The following definition expects stack
> items in this order:
> ( numerator denominator -- quotient )
> : /CHECK
> DUP 0= IF ." invalid " DROP
> ELSE /
> THEN ;
>
>
> Notice that we first have to DUP the denominator because the phrase
> 0= IF
>
> will destroy it in the process.
>
> Also notice that the word DROP removes the denominator if division
> won't be performed, so that whether we divide or not, the stack effect
> will be the same.
>
>
> The last two sentances confuse me.....can someone walk me through what
> the stack does? There seems to be a paradox where 0= destroys the
> denominator but DUPE is still needed to destory it?
I guess.
\ top bottom DUP
\ t b b 0=
\ t b flag IF
\ t b ( true case, b=0) ." invalid " DROP
\ top
\ t b ( false case, b<>0) /
\ quotient ;
If you'd be dividing by zero, you just don't do the division. You get
the numerator back, as if the denominator was 1. This is a wrong answer
but any answer would be wrong. You get the output message " invalid " to
tell you whatever result you're looking for is likely to be garbage.
Except for the pathetic warning you could do this without the branch but
I'm not sure it would be less confusing and I'm not sure there's any
hardware it would be quicker on.
DUP 2/ 0= 1 AND OR
/
The top line does this:
b DUP b b 2/ 0= ( b flag, flag is true if b=0|1, false otherwise) 1 AND
( { b 0 } | { 0|1 1 } )
OR ( b | 1 )
( t b|1 ) / ( t | q )
Yhere ought to be an architecture it's faster on. Pipeline stalls or
something like that. Maybe there isn't any.
If you actually want to do this on a modern Forth you might as well have
it THROW a division by zero exception. You could do:
( t b ) DUP 0= 0DIV
\ 0DIV would be a constant that stands for the division by zero
\ throw code which I don't remember.)
( t b throw-code|0 ) THROW
Then you could have code elsewhere that decides what to do if you have a
division by zero error, or you could let the system handle it. Many
Forths already have / trap division by zero, and you could still do it
yourself. Your code can do the THROW in case it runs on a system that
doesn't, and/or you can choose to do your own CATCH if there's something
useful you can do to handle it.


|