Announcement

Collapse
No announcement yet.

continued fractions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • continued fractions

    Hi,
    I am looking for help again. Here is my code:
    #COMPILE EXE
    #DIM ALL

    FUNCTION PBMAIN () AS LONG
    DIM arr(18) AS LONG
    DIM i AS LONG
    solution(SQR(7),arr())
    FOR i = 0 TO 17
    PRINT STR$(arr(i))+" ";
    NEXT i

    WAITKEY$

    END FUNCTION

    FUNCTION solution(x AS DOUBLE,arr() AS LONG)AS LONG

    DIM k AS LONG, a AS LONG
    DIM y AS DOUBLE
    FOR k = 0 TO 17
    a = INT(x)
    arr(k) = a
    y = x - a
    IF y = 0 THEN
    GOSUB endit
    ELSE
    x = 1/y
    END IF
    NEXT k
    endit:
    END FUNCTION
    The result is correct but the time is not! Has anyone an idea to make it faster?
    kind regards
    Volker

  • #2
    Volker,
    your program runs in under 2 microseconds. How fast do you want it?

    Some suggestions:
    Change:
    DIM y AS DOUBLE
    to
    REGISTER y AS EXT

    Don't use
    GOSUB endit
    instead use
    EXIT FUNCTION

    Paul.

    Comment


    • #3
      >your program runs in under 2 microseconds.

      I was afraid to try it myself, for fear I would become far less diplomatic than "How fast do you want it?"

      BTW, the "exit function" thing? That should be a command, not a suggestion. GOSUB should NEVER be used without a paired RETURN.



      MCM
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        GOSUB should NEVER be used without a paired RETURN.
        I think it is a matter of personal - or possibly corporate - taste. It has to be better programming practice to have a matching RETURN. ISTR Mr Zale commenting that at RETURN in a GOSUB is OK, so presumably the stack gets cleaned up correctly when you EXIT or END from a SUB or FUNCTION, irrespective of any GOSUB nesting, intended or otherwise.

        In the context given, the GOSUB does not appear to add any value at all.

        Comment


        • #5
          Re: continued fractions

          I am very sorry but my time is over 23 seconds. I would not have bothered you otherwise. Maybe it' s Vista !
          kind regards
          Volker

          Comment


          • #6
            Volker,
            are you sure you have posted the correct code?

            The answer I get is:
            2 1 1 1 4 1 1 1 4 1 1 1 4 1 1 1 4 1
            and it takes 1.5usecs to run here.

            If the same code is taking 23 seconds for you then there is a big problem somewhere.

            Paul.

            Comment


            • #7
              > In the context given, the GOSUB does not appear to add any value at all.

              It's clearly the wrong choice - in context.

              However, not coding a RETURN when you code a GOSUB is totally and absolutely unforgiveable, especially when dealing with a 'newbie,' and super-especially coming from someone who has been writing in BASIC as long as have you.

              Assuming the compiler is always going to be your momma and clean up after you is a very, very, very bad habit to acquire.
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Originally posted by Paul Dixon View Post
                ...code is taking 23 seconds...there is a big problem...
                Agreed. Bring up Task Manager and see if something is there, chewing up CPU usage.
                There are no atheists in a fox hole or the morning of a math test.
                If my flag offends you, I'll help you pack.

                Comment


                • #9
                  Originally posted by Michael Mattias View Post
                  ...totally and absolutely unforgiveable, especially when dealing with a 'newbie,' and super-especially coming from someone who has been writing in BASIC as long as have you.
                  Unworthy comment. Read and consider my post. Take your medication. Preferably, don't reply.

                  Comment


                  • #10
                    Code:
                    #compile exe
                    #dim all
                    
                    function solution(x as double,arr() as long)as long
                      dim k as long, a as long
                      dim y as double
                      for k = 0 to 17
                        a = int(x)
                        arr(k) = a
                        y = x - a
                        if y = 0 then
                          exit function
                        else
                          x = 1/y
                        end if
                      next k
                    end function
                    
                    function pbmain () as long
                      dim arr(18) as long
                      dim i as long
                      local T_Start, T_Stop as double
                      T_Start = timer
                      solution(sqr(7),arr())
                      for i = 0 to 17
                        print str$(arr(i))+" ";
                      next i
                      'sleep 100  'keep coming up 0 seconds on my PC
                                  'temporarily added sleep to check my code
                      T_Stop = timer
                      print : print str$(T_Stop - T_Start)
                    
                      waitkey$
                    
                    end function
                    Mod'd code to get run time. I get zero seconds using Timer.

                    Also fixed the gosub without return. Could have replaced gosub with goto, but goto starts arguements.

                    Cheers,
                    Dale

                    Comment


                    • #11
                      >Could have replaced gosub with goto, but goto starts arguements

                      Hmm, that happens in COBOL newsgroups, too, when code using GO TO is posted.

                      But nearly as big an argument as you can provoke by suggesting various uses of the full stop (the period, ".").

                      Thank goodness the comma (",") in COBOL is always optional and always 'documentary only!'
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        continued fractions

                        Thank you all.
                        I solved my time problem using another anti-virus-program.
                        Why are you so upset because used "goto". This is obviously possible and yields a correct result. "exit for" and "exit return" do here the same.
                        Kind regards
                        Volker
                        Last edited by Volker Vanoni; 18 Nov 2009, 09:33 AM. Reason: mistakes

                        Comment

                        Working...
                        X