| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| I was working on a Project Euler problem and ran into a problem using LEAVE to exit a DO ... LOOP --------------------------- : ?prime ( n -- f ) \ check is number is prime dup 1 > \ only test when positive if 1 begin 1+ 2dup / 2dup > \ treat negative numbers, if 2drop dup 1 then \ zero, and one as non-prime over * 2 pick = until = else FALSE then ; : numprimes ( a b -- num ) \ find number of primes generated by n^2+an+b dup ?prime \ make sure b is prime (n=0) if 1000 1 \ before testing n=1, 2, 3, etc do 2dup i rot over + * + ?prime not \ test if prime if 2drop i leave then \ if not, then return count loop \ else keep testing else 2drop 0 \ NOTE - should terminate loop early! then ; --------------------------- 1 41 numprimes . \ should print 40 2 41 numprimes . \ should print 1 instead, I get a nasty EXCEPTION 0xC0000005 ACCESS VIOLATION Error(9998). Tracing the code (using debug in win32forth) shows everything working as expected until the program tries to exit the loop by executing LEAVE. I've used LEAVE before, so any hints on what I've done wrong would be appreciated. Chris |
|
#2
| |||
| |||
| On Aug 7, 4:08*am, cjmcc <cjmccorm...@gmail.com> wrote: > instead, I get a nasty *EXCEPTION 0xC0000005 ACCESS VIOLATION > Error(9998). > It works okay on "Version: 6.10.05 Build: 2" What version are you using? -Brad |
|
#3
| |||
| |||
| On Aug 7, 5:08*am, cjmcc <cjmccorm...@gmail.com> wrote: > I was working on a Project Euler problem and ran into a problem using > LEAVE to exit a DO ... LOOP > > --------------------------- > : ?prime *( n -- f ) *\ check is number is prime > * dup 1 > * * * * * * * * * * * * * * * \only test when positive > * if *1 begin * 1+ 2dup */ *2dup > * * *\ treat negative numbers, > * * * * * if 2drop dup 1 then * * * * * \ zero, and one as non-prime > * * * * * over * 2 pick = > * * * * until *= > * else *FALSE > * then > ; > > : numprimes ( a b -- num ) * \ find number of primes generated by > n^2+an+b > * dup ?prime * * * * * * * * * * * * * *\ make sure b is prime (n=0) > * if * 1000 1 * * * * * * * * * * * * * \ before testing n=1, 2, 3, > etc > * * * *do *2dup *i rot over + * + > * * * *?prime not * * * * * * * * * * * \ test if prime > * * * *if *2drop i *leave *then * * * * \ if not, then return count > * * * *loop * * * * * * * * * * * * * * \ else keep testing > * else 2drop 0 * * * * * * * * * * * * *\ NOTE - should terminate loop > early! > * then > ; > --------------------------- > 1 41 numprimes . * * \ should print 40 > 2 41 numprimes . * * \ should print 1 > > instead, I get a nasty *EXCEPTION 0xC0000005 ACCESS VIOLATION > Error(9998). > > Tracing the code (using debug in win32forth) shows everything working > as > expected until the program tries to exit the loop by executing LEAVE. > > I've used LEAVE before, so any hints on what I've done wrong would be > appreciated. > > Chris I wonder if the line -- if 2drop i leave then -- leaves the stack in a different state than it should have been left. Steve |
|
#4
| |||
| |||
| On Aug 7, 6:04*pm, Brad Eckert <nospaambr...@tinyboot.com> wrote: > On Aug 7, 4:08*am, cjmcc <cjmccorm...@gmail.com> wrote:> instead, I geta nasty *EXCEPTION 0xC0000005 ACCESS VIOLATION > > Error(9998). > > It works okay on "Version: 6.10.05 Build: 2" > What version are you using? > > -Brad Are you sure? When I run it under the debugger I get the exception, but not when run normally. Since it reports the word TRACE as the faulty word then I think the debugger itself has a bug, though other code with LEAVE seems to work. George Hubert |
|
#5
| |||
| |||
| > > Are you sure? When I run it under the debugger I get the exception, > but not when run normally. Since it reports the word TRACE as > the faulty word then I think the debugger itself has a bug, > though other code with LEAVE seems to work. Yes, the debugger chokes on LEAVE. In fact, if I just put a LEAVE immediately after the DO it crashes. I guess the debugger's LEAVE got broken. -Brad |
|
#6
| |||
| |||
| On Aug 7, 7:08*am, cjmcc <cjmccorm...@gmail.com> wrote: > I was working on a Project Euler problem and ran into a problem using > LEAVE to exit a DO ... LOOP > ... > --------------------------- > 1 41 numprimes . \ should print 40 > 2 41 numprimes . \ should print 1 > instead, I get a nasty *EXCEPTION 0xC0000005 ACCESS VIOLATION > Error(9998). > ... Well, I did have a stack mistake pointed out to me in my initial ?prime routine which may have put the system in a confused state. With the following code everything seems to behave properly - including the debugger. ---------- \ code to support Project Euler problem 27 : ?prime ( n -- f ) \ check is number is prime dup 1 > \ only test when positive if 1 begin 1+ 2dup / 2dup > \ treat negative numbers, if 2drop dup 1 then \ zero, and one as non-prime over * 2 pick = until = else drop FALSE then ; : numprimes ( a b -- num ) \ find # of primes generated by n^2+an+b 1000 0 do 2dup i rot over + * + ?prime not \ test if prime if 2drop i leave then \ if not, then return count loop \ else keep testing ; ------- |
|
#7
| |||
| |||
| On 8 aug, 15:15, cjmcc <cjmccorm...@gmail.com> wrote: > Well, I did have a stack mistake pointed out to me in my > initial ?prime routine which may have put the system in a > confused state. With the following code everything seems > to behave properly - including the debugger. Works flawless on 4tH: \ code to support Project Euler problem 27 include lib/pickroll.4th : ?prime ( n -- f ) \ check is number is prime dup 1 > \ only test when positive if 1 begin 1+ 2dup / 2dup > \ treat negative numbers, if 2drop dup 1 then \ zero, and one as non-prime over * 2 pick = until = else drop FALSE then ; : numprimes ( a b -- num ) \ find # of primes made by n^2+an+b 1000 0 do 2dup i rot over + * + ?prime not \ test if prime if 2drop i leave then \ if not, then return count loop \ else keep testing ; 1 41 numprimes . cr \ should print 40 2 41 numprimes . cr \ should print 1 ------------------------------------ /home/user/habe/4th> 4th cxq euler27.4th 40 1 /home/user/habe/4th> Hans Bezemer |
|
#8
| |||
| |||
| In article <78c9c558-8de1-472f-88bc-292482473dbc@26g2000hsk.googlegroups.com>, The Beez' <hansoft@bigfoot.com> wrote: >On 8 aug, 15:15, cjmcc <cjmccorm...@gmail.com> wrote: >> Well, I did have a stack mistake pointed out to me in my >> initial ?prime routine which may have put the system in a >> confused state. With the following code everything seems >> to behave properly - including the debugger. >Works flawless on 4tH: > >\ code to support Project Euler problem 27 <Spoiler deleted> >------------------------------------ >/home/user/habe/4th> 4th cxq euler27.4th >40 >1 >/home/user/habe/4th> > > >Hans Bezemer Could you please mention spoiler in the subject line if you publish solutions to euler problems? Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- like all pyramid schemes -- ultimately falters. albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst |
|
#9
| |||
| |||
| On Aug 7, 8:40*pm, Brad Eckert <nospaambr...@tinyboot.com> wrote: > > Are you sure? When I run it under the debugger I get the exception, > > but not when run normally. Since it reports the word TRACE as > > the faulty word then I think the debugger itself has a bug, > > though other code with LEAVE seems to work. > > Yes, the debugger chokes on LEAVE. In fact, if I just put a LEAVE > immediately after the DO it crashes. I guess the debugger's LEAVE got > broken. > > -Brad Your right LEAVE was altered a while ago to use an address a cell greater than previously (along with other branches) and the debugger version wasn't updated (although other branches were). It's been sorted now (in the V6.13 developement version in our CVS). George Hubert |
|
#10
| |||
| |||
| On 10 aug, 22:37, Albert van der Horst <alb...@spenarnc.xs4all.nl> wrote: > Could you please mention spoiler in the subject line > if you publish solutions to euler problems? That's alright, Albert, but why pick on me? I was just posting a reacting to a previous post! Hans Bezemer |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.