Exponentiation (`^`) on a non-US keyboard

I am lost.
What changed so the simple algebra like 2ˆ2 does not work anymore (Julia v1.6.3) in the REPL (on MacOS)?

And how would I draw the power of a int or a float?

I get

julia> 2ˆ2
ERROR: UndefVarError: ˆ2 not defined
Stacktrace:
 [1] top-level scope
   @ REPL[31]:1

And

julia> 2 ˆ 2
ERROR: syntax: extra token "ˆ" after end of expression
Stacktrace:
 [1] top-level scope
   @ none:1

And

2 ˆ2
ERROR: syntax: extra token "ˆ2" after end of expression
Stacktrace:
 [1] top-level scope
   @ none:1

and

julia> 2ˆ 2
ERROR: syntax: extra token "2" after end of expression
Stacktrace:
[1] top-level scope
@ none:1

and finally

julia>2.0ˆ2.0
ERROR: syntax: extra token "0.0" after end of expression
Stacktrace:
[1] top-level scope
@ none:1

Thanks for your shard of enlightment.

You have probably copy/pasted the ˆ from somewhere, try this one:

2^2

or the single ^.
Copy/paste one of my suggestions.

You are using “Modifier Letter Circumflex Accent” with code

julia> Char('ˆ')
'ˆ': Unicode U+02C6 (category Lm: Letter, modifier)

You need CIRCUMFLEX ACCENT

julia> Char('^')
'^': ASCII/Unicode U+005E (category Sk: Symbol, modifier)

(edited: using Char instead of UInt16, :+1: to @Palli )

2 Likes

It still may be a MacOS thing, so someone with that system may know better…

Phenomenal!
It just had to be something like this.
In fact, I am new to the Mac-keyboard and it is these small things that make doubt this path.

1 Like

Well, this can happen in every system,e.g. R REPL just the same:

> 2**2
[1] 4
> 2^2
[1] 4
> 2ˆ2
Fehler: unerwartete Eingabe in "2ˆ"

No doubts appropriate! Instead, delight is today’s feeling! :wink:

1 Like

Or Python

>>> 2**2
4
>>> 2^2
0
>>> 2ˆ2
  File "<stdin>", line 1
    2ˆ2
     ^
SyntaxError: invalid syntax

I’m conflicted here. With the correct ASCII operator ^ this works, but Julia does provide Unicode options in many cases, and could also support ˆ “Modifier Letter Circumflex Accent” for this. I’m not sure what else it could mean usefully. Does anyone use it for anything else?

For me ^ is the most natural power operator and ** just bad, that other languages like Python have.

Note, ^ is xor in other some other languages (e.g. C and Python, so can be a pitfall porting code), while Julia has operator for it written \xor tab, or as a function xor(a, b). All operators in Julia are ASCII by default (often Unicode also) in Julia except that one, there the ASCII version is a function only.

My main struggle is actually the MacBook keyboard where the ASCI tilde and ^ are hidden from simple key strokes.
My left pinky is just getting board while the others are starting emacs like yoga exercises without any emacs in sight.

4 Likes

If it’s not on the keyboard that seems like an argument for some workaround… If Julia does it allowing the other ˆ it makes for some slight visual distinction in code bases, why I’m conflicted. It’s most likely also a technical breaking change. Maybe there’s some editor support available that maps it to ^? You can’t be the first to run into this problem… Xor is a rarely used function, so ^ for it in Python and C isn’t a deal breaker, but ^ (for power) is very common so Julia must be easy. Possibly it could be a warning, and then only in the REPL.

Resolved the twist!
My keyboard was set to en-US international instead of just en-US.

https://apple.stackexchange.com/questions/234352/how-do-i-input-instead-of-ˆ

2 Likes

2 posts were split to a new topic: ^ vs exp for exponentiation

By the way, it’s quite easy to add your own REPL shortcuts, if there’s a particular character you need to type which isn’t in the REPL’s built-in tab completion dictionary:

For example, to have \power expand to ^:

using REPL
REPL.REPLCompletions.latex_symbols["\\power"] = "\u005e"

completions-1

Put it in your ~/.julia/config/startup.jl file to run it every time you start a REPL.

2 Likes

neat trick.
You would not have a table or list of Unicodes you would recommend, would you?

:confused: According to the images of current Mac laptops on Apple’s web site, these characters are where they always are on a US keyboard: at the upper left (with shift-`) for ~ and at shift-6 for ^:
image

Well, I in fact have a physical Swiss-German keyboard and was running the en-US international layout.
Little did I know the en-US layout would be what I wanted and my fingers are used to.
But that was a quick fix.

1 Like

No it could not. ˆ (U+02C6) is a modifier letter in Unicode (category Lm), and all modifier letters are allowed as part of identifiers in Julia as long as they follow an ordinary letter. e.g. xˆ2 is a valid identifier:

julia> xˆ2 = 16
16

julia> √xˆ2
4.0

Changing Julia to parse ˆ as a binary operator would therefore be a breaking change, and is hence technically disallowed in Julia 1.x.

(This is a weird case, so it’s not clear if anyone is actually using U+02C6 for identifiers in real code, and maybe it could be argued as a “minor” change for Julia 1.x. On the other hand, the ordinary ^ is an ASCII character (U+005E), and it is reasonable to require that anyone programming in any modern programming language be capable of typing it.)

4 Likes

In fact, it doesn’t have to follow an ordinary letter:

julia> const ˆ2 = exp2
exp2 (generic function with 10 methods)

julia> ˆ2(16)
65536.0

This is also allowed in Python 3:

>>> xˆ2 = 16
>>> xˆ2
16
>>> from numpy import exp2 as ˆ2
>>> ˆ2(16)
65536.0

In fact, I suspect that most modern computer languages allow this. The reason is that the general Unicode recommendations on identifier characters (Unicode Annex #31) suggest allowing modifier letters to start identifiers (property ID_Start). They do include a caveat that languages “may” want to specially handle modifier letters that resemble punctuation or other special characters, but I don’t know whether any languages do this for U+02C6.

1 Like