Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Forth > Re: confusing b...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 4 Topic 3787 of 4288
Post > Topic >>

Re: confusing bit in ch4 of starting forth

by Jonah Thomas <jethomas5@[EMAIL PROTECTED] > Feb 29, 2008 at 05:19 PM

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.
 




 4 Posts in Topic:
confusing bit in ch4 of starting forth
gavino <gavcomedy@[EMA  2008-02-29 12:08:18 
Re: confusing bit in ch4 of starting forth
Frank Buss <fb@[EMAIL   2008-02-29 23:01:23 
Re: confusing bit in ch4 of starting forth
Jonah Thomas <jethomas  2008-02-29 17:19:02 
Re: confusing bit in ch4 of starting forth
gavino <gavcomedy@[EMA  2008-02-29 14:49:04 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 8:33:04 CDT 2008.