# Unexpected behaviour with integer sqrt and integer ^

**URL:** <https://discourse.julialang.org/t/unexpected-behaviour-with-integer-sqrt-and-integer/76897>\
**Category:** General Usage\
**Created:** [February 22, 2022, 10:11am UTC](https://discourse.julialang.org/t/unexpected-behaviour-with-integer-sqrt-and-integer/76897 "2022-02-22T10:11:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Kleinbeck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin_kleinbeck/32/21278_2.png) [@Kevin\_Kleinbeck](https://discourse.julialang.org/u/Kevin_Kleinbeck)\
**Post date:** [February 22, 2022, 10:11am UTC](https://discourse.julialang.org/t/unexpected-behaviour-with-integer-sqrt-and-integer/76897/1 "2022-02-22T10:11:12Z")

</div>

**Motivation:**  
I stumbled upon

```julia
√(16)^16 # 0.0

```

and

```julia
√(17)^17 # DomainError with -2.8632214305930583e18:

```

**The reason:** This is of course due to integer arithmetic. On my machine:

```julia
16^16 # 0
17^17 # -2863221430593058543

```

**Why I think this is unexpected behaviour**  
I, and I guess most programmers, read `√(16)^16` as:

1. call the method `√` with argument 16
2. raise the output to the power 16.

This would indeed be the case if I would have used

```julia
sqrt(16)^16 # 4.294967296e9

```

On the other hand, I can imagine, that Julia replaces `√(x)` by `x^(0.5)` under the hood. However, `^` acts right associative, so then I would expect something along the line

```julia
√(16)^16 == 16^0.5^16 == 16^8.0 == 4.294967296e9

```

In short, I would not have expected integer arithmetic to matter for `√(16)^16`, as I expected `√(16)` naturally promotes to Float64 and the power operation happens after this.

**Relevance and next steps?**  
I found this in a loop:

```julia
for M in 1:20
  #...
  det = √(M)^M
end

```

i.e., in a setting where one naturally works with integers, so I guess this can be relevant. The fixes are of course obvious: `sqrt(M)^M` or `√(1.0 * M)^M`.

I tested this both on Julia 1.6.3 and 1.7.2. on a MacBook Pro 2017.

Is this indeed unexpected behaviour and so far unknown? In that case, should I open an Issue on Github?

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [February 22, 2022, 10:27am UTC](https://discourse.julialang.org/t/unexpected-behaviour-with-integer-sqrt-and-integer/76897/2 "2022-02-22T10:27:37Z")

</div>

The problem seems to be that that √ is a prefix operator with lower precedence than ^, resulting in `sqrt(16^16)` being the code that is run. The strange thing is that looking at the [precedences](https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm) I would interpret it as √ having higher precedence since it is defined on line 99 compared to ^ which is on line 30?

```julia
julia> dump(:(√(16)^16))
Expr
  head: Symbol call
  args: Array{Any}((2,))
    1: Symbol √
    2: Expr
      head: Symbol call
      args: Array{Any}((3,))
        1: Symbol ^
        2: Int64 16
        3: Int64 16

julia> √16^16 # Parenthesis doesn't matter here since √ is parsed as an operator
0.0

julia> (√16)^16 # This forces sqrt to be run first
4.294967296e9

```

---

<div class="post-metadata">

**Author:** ![Bernard\_GODARD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernard_godard/32/4155_2.png) [@Bernard\_GODARD](https://discourse.julialang.org/u/Bernard_GODARD)\
**Post date:** [February 22, 2022, 10:27am UTC](https://discourse.julialang.org/t/unexpected-behaviour-with-integer-sqrt-and-integer/76897/3 "2022-02-22T10:27:44Z")

</div>

sqrt and the sqrt symbol are the same function

However operator precedence in the parser might be the problem:

```julia
julia> Base.operator_precedence(:^)
15

julia> Base.operator_precedence(:√)
0

```

^ is one of the operator with highest priority.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [February 22, 2022, 12:23pm UTC](https://discourse.julialang.org/t/unexpected-behaviour-with-integer-sqrt-and-integer/76897/4 "2022-02-22T12:23:23Z")

</div>

> [@albheim](#):
>
> The strange thing is that looking at the [precedences](https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm) I would interpret it as √ having higher precedence since it is defined on line 99 compared to ^ which is on line 30?

No, the documentation clearly states that `^` has higher precedence:

[https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity)

---

<div class="post-metadata">

**Author:** ![Kevin\_Kleinbeck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin_kleinbeck/32/21278_2.png) [@Kevin\_Kleinbeck](https://discourse.julialang.org/u/Kevin_Kleinbeck)\
**Post date:** [February 22, 2022, 12:28pm UTC](https://discourse.julialang.org/t/unexpected-behaviour-with-integer-sqrt-and-integer/76897/5 "2022-02-22T12:28:37Z")

</div>

> julia\> Base.operator\_precedence(:^)  
> 15  
> julia\> Base.operator\_precedence(:√)  
> 0

I don’t think this is representative, see:

> julia\> Base.operator\_precedence(:\*)  
> 12  
> julia\> √(4)\*4  
> 8.0

i.e., `√` has to have a higher precedence as 0.

---

<div class="post-metadata">

**Author:** ![Bernard\_GODARD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernard_godard/32/4155_2.png) [@Bernard\_GODARD](https://discourse.julialang.org/u/Bernard_GODARD)\
**Post date:** [February 22, 2022, 12:58pm UTC](https://discourse.julialang.org/t/unexpected-behaviour-with-integer-sqrt-and-integer/76897/6 "2022-02-22T12:58:16Z")

</div>

```julia
help?> Base.operator_precedence
  operator_precedence(s::Symbol)

  Return an integer representing the precedence of operator s, relative to other operators. Higher-numbered operators take precedence over lower-numbered operators. Return 0 if s is not a valid operator.

```

yes 0 means invalid operator. I should have read the documentation. Then I do not know about how the parser handles the precedence of \sqrt wrt to \* relative to \sqrt wrt to ^

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 22, 2022, 1:47pm UTC](https://discourse.julialang.org/t/unexpected-behaviour-with-integer-sqrt-and-integer/76897/7 "2022-02-22T13:47:31Z")

</div>

I would guess that `√` was just another name for `sqrt`, this is, it is just a function name and not an operator but apparently it can be used without parenthesis, so it does not seem to be the case. The documentation uses the term “prefix operator”.

```julia
"√" can be typed by \sqrt<tab>

search: √

  sqrt(x)

  Return \sqrt{x}. Throws DomainError for negative Real arguments. Use complex
  negative arguments instead. The prefix operator √ is equivalent to sqrt.

```

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [February 22, 2022, 3:47pm UTC](https://discourse.julialang.org/t/unexpected-behaviour-with-integer-sqrt-and-integer/76897/8 "2022-02-22T15:47:39Z")

</div>

Nice! Went directly to source to try to find it but reasonable that there is good docs for it 🙂

My confusion was referring to the comment on the first line in the linked file which stated `;; Operator precedence table, lowest at top` which I interpreted as lower precedence at top of file. Not sure how this is supposed to be interpreted, but probably not as I did at least…

---

<div class="post-metadata">

**Author:** ![Kevin\_Kleinbeck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin_kleinbeck/32/21278_2.png) [@Kevin\_Kleinbeck](https://discourse.julialang.org/u/Kevin_Kleinbeck)\
**Post date:** [February 22, 2022, 4:08pm UTC](https://discourse.julialang.org/t/unexpected-behaviour-with-integer-sqrt-and-integer/76897/9 "2022-02-22T16:08:14Z")

</div>

Okay, it seem it is now clear what is happening… so my question is: does it make sense like that 😃

Idk, but I feel like `√` should have higher precedence than `^` after observing the effects of the converse. Also `√(16)` feels more like a function call than applying a right associative operator. I quickly scanned parts of the documentation and so far I exclusively found examples with the `√(x)` syntax instead of `√x`, so the standard seems to support the “method call” interpretation.

Are there other opinions?

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [February 22, 2022, 5:14pm UTC](https://discourse.julialang.org/t/unexpected-behaviour-with-integer-sqrt-and-integer/76897/10 "2022-02-22T17:14:19Z")

</div>

I believe that because `√` is a prefix operator, it should be used as `√x` not `√(x)`. For example, `Meta.parse("√x") == Meta.parse("√(x)") == :(√x)`. If the documentation uses it otherwise, it should be changed.

> [@Kevin\_Kleinbeck](#):
>
> I quickly scanned parts of the documentation and so far I exclusively found examples with the `√(x)` syntax instead of `√x` , so the standard seems to support the “method call” interpretation.

Could you point us to specific examples? I found 71 occurrences of `√` in the JuliaLang/Julia codebase (including /doc) only three of which use parentheses:

“`julia> .√(1:4)`”  
“`julia> √(a^2 + a^2) # a^2 overflows`”  
“compositions of unary operators are parsed with right-associativity, e. g., `√√-a` as `√(√(-a))`.”

---

<div class="post-metadata">

**Author:** ![Kevin\_Kleinbeck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin_kleinbeck/32/21278_2.png) [@Kevin\_Kleinbeck](https://discourse.julialang.org/u/Kevin_Kleinbeck)\
**Post date:** [February 23, 2022, 7:32am UTC](https://discourse.julialang.org/t/unexpected-behaviour-with-integer-sqrt-and-integer/76897/11 "2022-02-23T07:32:55Z")

</div>

> [@Kevin\_Kleinbeck](#):
>
> I quickly scanned parts of the documentation

Guess lucky-me directly found the few examples of `√(x)`. To be fair, they are on the [mathematics](https://docs.julialang.org/en/v1/base/math/) page, so it was an obvious place to search for 💁‍♂️
