Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unary minus precedence? #565

Closed
dronir opened this issue Mar 11, 2012 · 6 comments
Closed

Unary minus precedence? #565

dronir opened this issue Mar 11, 2012 · 6 comments
Assignees
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@dronir
Copy link
Contributor

dronir commented Mar 11, 2012

Not sure if this is intentional or not, but it caused me some confusion. If it's intentional, I'd be interested to hear the rationale.

The precedence of the unary minus seems to be over exponentiation such that -(x-y)^2 equals (-(x-y))^2, so for example -(1-3)^2 == 4. This differs from at least Python, where -(x-y)**2 equals -((x-y)**2).

Might be a matter of taste (as Python is my major language), but for me this latter way is more intuitive, and makes for prettier expressions. I was writing something with a gaussian function exp(-(x-a)^2) and got quite unexpected numbers out at first. Had to write it exp(-((x-a)^2)) which looks a bit cluttered.

I think this should at least be mentioned in the manual somewhere (at least I couldn't find much about operator precedence).

@ghost ghost assigned JeffBezanson Mar 11, 2012
@JeffBezanson
Copy link
Member

Good catch. I'd say this is a bug, because:

julia> -3^2
-9

julia> -(4-1)^2
9

The first one is the one we intended, and these clearly shouldn't be different.

@JeffBezanson
Copy link
Member

Oh, I know why this happened. We made -(x) function call syntax, not negation of a parenthesized expression. That way you can call - using -(x,y). So this is using the same precedence as f(x)^2, where f is clearly called first. This was desired for writing definitions, such as

-(x, y) = ...

Otherwise it would have to be written as

(-)(x, y) = ...

But I think this was the wrong tradeoff, since you hardly ever want to call or define - using prefix syntax, compared to the importance of negating stuff.
@StefanKarpinski what do you think?

@StefanKarpinski
Copy link
Member

I guess if we switch this back then methods for - will have to be written as (-)(x) = .... I guess that's ok.

@JeffBezanson
Copy link
Member

We might be able to have both, if we're willing to assume you never want to negate a tuple.
-(x) is the same no matter how you parse it (always a 1-argument call to minus), but we could make tuples a special case so that -(x,y) by itself is 2-argument call to minus instead of negation of a tuple. The only nasty thing there is that -(x,y) would work, but -(x,y)^2 would not since that would try to square the tuple first.

@StefanKarpinski
Copy link
Member

Seems reasonable enough to me. There will inevitably be people who try -(x,y)^2 and are flummoxed when it doesn't work, but I think that's a weird enough thing to try that having it not work is ok.

@oviedorodolfo

This comment has been minimized.

Keno pushed a commit that referenced this issue Oct 9, 2023
These get called in the absence of knowledge about the second input,
so protecting them with `invokelatest` avoids invalidation risk.
ViralBShah pushed a commit that referenced this issue Nov 12, 2024
Stdlib: SparseArrays
URL: https://github.com/JuliaSparse/SparseArrays.jl.git
Stdlib branch: main
Julia branch: master
Old commit: 0dd8d45
New commit: 14333ea
Julia version: 1.12.0-DEV
SparseArrays version: 1.12.0
Bump invoked by: @ViralBShah
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaSparse/SparseArrays.jl@0dd8d45...14333ea

```
$ git log --oneline 0dd8d45..14333ea
14333ea Break recursion (#579)
07cf4a6 Update ci.yml (#578)
33491e0 added diagonal-sparse multiplication (#564)
8f02b7f doc: move solvers doc to `src\solvers.md` (#576)
485fd4b Inline sparse-times-dense in-place multiplication (#567)
f10d4da added specialized method for 3-argument dot with diagonal matrix (#565)
70c06b1 Diagonal-sandwiched triple product for SparseMatrixCSC (#562)
313a04f Change default QR tolerance to match SPQR (#557)
81d49e9 Update ci.yml (#558)
```

Co-authored-by: Dilum Aluthge <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants