Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem using basic goto example in Mata

    Dear Statalisters,

    I need to use the goto syntax to translate a Fortran program in Mata. There are obviously better solutions than using goto, but the Fortran program relies heavily on this goto syntax and so I want to keep it for a first translation in Mata. I tried to run the basic goto example given on the Mata help page (Mata Reference Manual, p 128) with Mata in interactive mode and the code crashes:

    mata:

    a = 4/3
    s10: b = a - 1
    c = 3*b
    eps = abs(c-1)
    if(eps==0) goto s10

    end

    The mistake is "Unexpected end of line". Why is the end statement ambiguous here?

    Regards,

    Yannick Guyonvarch

  • #2
    I think the problem comes from using the if statement interactively. If you wrap your code in a function you won't get this error message. Otherwise you have to use ; if I remember correctly

    Comment


    • #3
      Thanks! However, adding the ; has no effect in this specific case.

      Comment


      • #4
        Ok. If you add two semi colons you get a funny system error. Maybe goto is not designed to work interactively, although I couldn't read in the manual that you can't.

        Comment


        • #5
          1. Please, no goto in the 21st century:
          Code:
          mata:
          
          a = 4/3
          do {
              b = a - 1
              c = 3*b
              eps = abs(c-1)
          } while (eps==0)
          
          end
          Reference
          mata do (p.110).

          There is exactly 1 (one) mata file in Stata 13-th installation, which uses goto, and it could have been avoided there too by using a simple else.

          2. I am not comfortable with the comparison of reals in such a fashion. What is intended? Remember that epsilon is not zero in practice. Perhaps eps should be compared with some small number, as in the example in the manual? Or eps is the remainder from division?

          3. I also failed to see what the loop is doing. The value of a is set externally, everything else is deduced from it in the body of the loop. By the time you get to the branching statement, eps is uniquely defined for every given a:
          Code:
          eps=abs(3a-4)
          so the loop is either one iteration or an eternal loop . Something is wrong here.

          Best, Sergiy Radyakin

          Comment

          Working...
          X