The observed issue:
$a = Formula("-sqrt(x)");
and then when I print `$a`, what comes out is
The parentheses are undesirable. The same happens with -sin(x), -ln(x), etc. so this is not specific to sqrt.
What I've been able to turn up so far:
I have tracked this down to:
In this situation, the unary minus has a property `precedence` that is 6 and the function sqrt has a property called `parenPrecedence` that is 5. Because the 6 is greater than the 5, parentheses are inserted. Also worth noting, the function also has a property `precedence` that is 2.9, but that is not relevant for inserting the parens.
These properties are set at:
There are some things I do not understand. Maybe the most fundamental is why is the precedence for a function so low (2.9)? If I make Formula("sqrt4^3"), I get 8. So it is giving the sqrt higher precedence than the ^, which is what I would expect. And yet sqrt has precedence 2.9 and ^ has precedence 7. So I do not understand something fundamental there.
But the underlying question is, could we change things so that Formula("-sqrt(x)") comes out by default without extra parens? Is there some reason that would have a bad side effect? If I understood Function.pm#L310 better, that would help.