ERROR: syntax: "±" is not a unary operator

350 views
Skip to first unread message

Kaj Wiik

unread,
Jun 22, 2016, 8:08:21 AM6/22/16
to julia-users

I have a strange problem using ± and Unicode symbols as variables when placed at the front of equation. Although they seem not to be defined in Main, they behave differently from e.g. α (but e.g. 2*± does work). See below. Any suggestions?

               _

   _       _ _(_)_     |  A fresh approach to technical computing
 
(_)     | (_) (_)    |  Documentation: http://docs.julialang.org
   _ _   _
| |_  __ _   |  Type "?help" for help.
 
| | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.4.5 (2016-03-18 00:58 UTC)
 _/
|\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-unknown-linux-gnu

julia
> α=-1.0
-1.0

julia
> α*2
-2.0

help
?> ±
search
:

Couldn't find ±
Perhaps you meant *, α, !, $, %, &, +, -, /, :, <, >, I, \, ^, e, |, ~, ×, ÷, γ or π
ERROR: "±" is not defined in module Main
 in error at error.jl:21
 in which_module at ./reflection.jl:315
 in call at ./docs/bindings.jl:8

julia> ±=-1.0
-1.0

julia> ±*2
ERROR: syntax: "±" is not a unary operator

julia> ∓=-1.0
-1.0

julia> ∓*2
ERROR: syntax: "∓" is not a unary operator

julia> 2*±
-2.0

julia> 2*∓
-2.0


David van Leeuwen

unread,
Jun 22, 2016, 9:02:57 AM6/22/16
to julia-users
It probably is defined as an operator, but without definition.

julia> ±(a,b) = (a+b, a-b)
± (generic function with 1 method)

julia> 3±4
(7,-1)



your assignment probably overrides the default operator. 


---david

Kaj Wiik

unread,
Jun 22, 2016, 9:34:00 AM6/22/16
to julia-users
Yes, it is probably a binary operator but searches fail to find it.

Any use would also clash with Measurements.jl, so it'd be a bad idea anyway :-).

Thanks,
Kaj

Steven G. Johnson

unread,
Jun 22, 2016, 10:03:09 AM6/22/16
to julia-users


On Wednesday, June 22, 2016 at 2:34:00 PM UTC+1, Kaj Wiik wrote:
Yes, it is probably a binary operator but searches fail to find it.

Certain symbols are parsed as operators, and others are parsed as identifiers.  This is independent of whether that symbol is defined in Base.

For example α is parsed as an identifier (which is not defined in Base).  ± and ⊕, on the other hand, are parsed as binary operators (which are also not defined in Base).

For something parsed as a binary operator, if you want to give it a definition you should define it as a two-argument function, e.g. ⊕(x,y) = x .+ 2y ....   then e.g. a⊕b will correspond to the function call ⊕(a,b).

Kaj Wiik

unread,
Jun 22, 2016, 3:39:35 PM6/22/16
to julia-users
To wrap this item up, it would be good to know where the distinction (parsing) is done in sources?

Thanks,
Kaj

Stefan Karpinski

unread,
Jun 22, 2016, 4:15:44 PM6/22/16
to Julia Users
Side comment: ± should probably be allowed as a unary operator.
Reply all
Reply to author
Forward
0 new messages