Announcement

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

  • Inconsistent error message within and outside of functions

    I came across very inconsistent error messages while writing a program which picks a maximum given a set of conditions. The bug (?) appeared when a subscript was invalid, mata returned the correct error code but pointed to a _editvalue() several lines above. Figuring the error out took me some time because of the inconsistent messages. When I write down the function in a sandbox, mata also returns inconsistent error messages.

    A working example (just an example!) of the code is:

    Code:
    mata:
    
    function checkBug(real matrix mat)
    {
        N = rows(mat)    
        maxindex(mat,1,max_indic=.,tmp=.)
        _editvalue(mat,0,.)
        "msg after _editvalue()"
        if (mat[max_indic+1,1] == . ) check1 = 0
        return(max_indic)
    }
    Clearly, the code should fail if the maximum is the last element in mat. This is a coding error. I would expect the error message that a subscript in invalid.

    If I run

    Code:
    : checkBug((1,2,3,4,5,6)')
      msg after _editvalue()
                _editvalue():  3301  subscript invalid
                  checkBug():     -  function returned error
                     <istmt>:     -  function returned error
    (1 line skipped)
    The message is confusing in several ways. First of all the function which reports the error is_editvalue(), but mata tells me the right number of lines skipped. Secondly the code after (!) _editvalue runs. Obviously, the error is that the subscript in if (mat[max_indic+1,1] == . ) check1 = 0 is invalid. So part of the error message is correct, it just points to the wrong function.

    If I replace _editvalue(mat,0,.) with rnormal(max_indic,1,0,1) then mata returns

    Code:
     rnormal():  3301  subscript invalid
    So pointing again to the wrong function.

    I then checked the code outside of a function, so I coded:

    Code:
    mata:
        mat = (1,2,3,4,5,6)'
        N = rows(mat)    
        maxindex(mat,1,max_indic=.,tmp=.)
        _editvalue(mat,0,.)
        if (mat[max_indic+1,1] == . ) check1 = 0
    end
    Here the error message became:

    Code:
    unexpected end of line
    (0 lines skipped)
    I somewhat understand that mata has a problem because mat[max_indic+1,1], but why does it report unexpected end of line? Should it not realise that it is a invalid subscript?

    If the code is instead:

    Code:
    mata:
        mat = (1,2,3,4,5,6)'
        N = rows(mat)    
        maxindex(mat,1,max_indic=.,tmp=.)
        _editvalue(mat,0,.)
        (mat[max_indic+1,1] == . )    
    end
    or

    Code:
    mata:
        mat = (1,2,3,4,5,6)'
        N = rows(mat)    
        maxindex(mat,1,maxi=.,tmp=.)
        _editvalue(mat,0,.)
        i = 0
        check = 0
        while (check==0) {
            i++
            max_indic = maxi[i,1]
            if (mat[max_indic+1,1] == . ) check1 = 0
        }    
    end
    Then the error message is "correct":

    Code:
                     <istmt>:  3301  subscript invalid
    (0 lines skipped)
    My question is, what is the reason for the difference if the code is within a function or not? I do know that mata compiles functions first, but why does the error point to the wrong command? In the sandbox mode out of a function, the error leads to different messages as well.

    Any help would be highly appreciated.




  • #2
    JanDitzen
    I can replicate your problem. If I replace _editvalue(mat,0,.) with
    Code:
    mat = editvalue(mat,0,.)
    I receive the expected error message:
    Code:
    : checkBug((1,2,3,4,5,6)')
      msg after _editvalue()
                  checkBug():  3301  subscript invalid
                     <istmt>:     -  function returned error
    It might be worth reporting this to Stata's technical support team.
    https://twitter.com/Kripfganz

    Comment

    Working...
    X