# Why does \`sqrt\` behaves differently than \`√\`?

**URL:** <https://discourse.julialang.org/t/why-does-sqrt-behaves-differently-than/65328>\
**Category:** New to Julia\
**Created:** [July 26, 2021, 7:47pm UTC](https://discourse.julialang.org/t/why-does-sqrt-behaves-differently-than/65328 "2021-07-26T19:47:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![HugoTrentesaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugotrentesaux/32/12583_2.png) [@HugoTrentesaux](https://discourse.julialang.org/u/HugoTrentesaux)\
**Post date:** [July 26, 2021, 7:47pm UTC](https://discourse.julialang.org/t/why-does-sqrt-behaves-differently-than/65328/1 "2021-07-26T19:47:21Z")

</div>

Despite being equal, they behave differently:

```julia
julia> sqrt.([4, 9])
2-element Vector{Float64}:
 2.0
 3.0

julia> sqrt == √
true

julia> √.([4, 9])
ERROR: syntax: invalid identifier name "."
Stacktrace:
 [1] top-level scope
   @ none:1

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 26, 2021, 7:48pm UTC](https://discourse.julialang.org/t/why-does-sqrt-behaves-differently-than/65328/2 "2021-07-26T19:48:23Z")

</div>

This is just a parsing thing. You want

```julia
julia> (√).([4, 9])
2-element Vector{Float64}:
 2.0
 3.0

```

---

<div class="post-metadata">

**Author:** ![HugoTrentesaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugotrentesaux/32/12583_2.png) [@HugoTrentesaux](https://discourse.julialang.org/u/HugoTrentesaux)\
**Post date:** [July 26, 2021, 7:57pm UTC](https://discourse.julialang.org/t/why-does-sqrt-behaves-differently-than/65328/3 "2021-07-26T19:57:15Z")

</div>

Then where can I read Julia’s parsing implementation? I looked around in [julia/src/ast.c](https://github.com/JuliaLang/julia/blob/696cb1ba7bf03b7c275a870e5f4229c986e1383f/src/ast.c#L1324) but I’m not familiar with this part of code and can not find the precise location which is in cause.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 26, 2021, 7:58pm UTC](https://discourse.julialang.org/t/why-does-sqrt-behaves-differently-than/65328/4 "2021-07-26T19:58:59Z")

</div>

I couldn’t tell you the cause either. But I do know that for unary or binary operators, to broadcast them you generally need to wrap them in parentheses.

---

<div class="post-metadata">

**Author:** ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)\
**Post date:** [July 26, 2021, 8:05pm UTC](https://discourse.julialang.org/t/why-does-sqrt-behaves-differently-than/65328/5 "2021-07-26T20:05:06Z")

</div>

The actual parser is here: [julia/julia-parser.scm at master · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm)

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 26, 2021, 8:19pm UTC](https://discourse.julialang.org/t/why-does-sqrt-behaves-differently-than/65328/6 "2021-07-26T20:19:06Z")

</div>

Yeah, strangely with infix operators the `.` goes in front of the operator:

```julia
julia> .√([4, 9])
2-element Vector{Float64}:
 2.0
 3.0

```

It’s like the binary `.+` and `.*`. That said, given that this is a syntax error… I wonder if we couldn’t support either placement.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 26, 2021, 8:26pm UTC](https://discourse.julialang.org/t/why-does-sqrt-behaves-differently-than/65328/7 "2021-07-26T20:26:59Z")

</div>

I think the ship has sailed on that. After 1.6, `.+` and `.*` are defined as `Base.Broadcast.BroadcastFunction(+)`. Actually so is square root

```julia
julia> .√
Base.Broadcast.BroadcastFunction(sqrt)

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 26, 2021, 8:34pm UTC](https://discourse.julialang.org/t/why-does-sqrt-behaves-differently-than/65328/8 "2021-07-26T20:34:47Z")

</div>

I don’t see why that changes anything. `.√([3,4])` doesn’t actually create a `BroadcastFunction`. I’m just saying maybe we could also support `√.([3,4])` like a normal function named `√`?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 26, 2021, 8:49pm UTC](https://discourse.julialang.org/t/why-does-sqrt-behaves-differently-than/65328/9 "2021-07-26T20:49:57Z")

</div>

> [@HugoTrentesaux](#):
>
> Despite being equal, they behave differently:

Just to be clear, the reason they behave differently is that `sqrt` is an ordinary function name while `√` is a **unary operator**.

That means, for example, that you can apply `√` without parentheses:

```julia
julia> √2
1.4142135623730951

```

and like all operators in Julia it is “vectorized” by putting a `.` _before_ the operator (a convention adopted from Matlab):

```julia
julia> .√[4,9]
2-element Vector{Float64}:
 2.0
 3.0

```

I agree with @mbauman that, _if_ you use a binary/unary operator `OP` with parens, as `OP(...)`, then it would not be crazy to allow `OP.(...)` as an alternative vectorization syntax similar to ordinary functions. This doesn’t seem like it would be breaking since it is currently a syntax error.
