matlabdivisionoperator-precedencefunction-handle

Order of calculation for multiple successive division operations in a single term


Why does this function handle in Matlab g = @(x)(4*x^5-A)/5/x^4; correspond to g(x) = (4x^5-A)/5x^4 and not to (4x^5-A)/(5/x^4)?


Solution

  • What you are observing is the left associativity of the division operator. Maybe we should simplify the example first, no anonymous function, just the operators:

    >> 5/5/5
    ans =
        0.2000
    >> (5/5)/5
    ans =
        0.2000
    >> 5/(5/5)
    ans =
         5
    >> 
    

    There isn't really a logical reason behind, but all programming languages I know have math operators like / left associative. * is also left associative, but it usually does not matter. When it comes to order of operation, also consider the precedence.