I stumbled upon
√(16)^16 # 0.0
√(17)^17 # DomainError with -2.8632214305930583e18:
The reason: This is of course due to integer arithmetic. On my machine:
16^16 # 0 17^17 # -2863221430593058543
Why I think this is unexpected behaviour
I, and I guess most programmers, read
- call the method
√with argument 16
- raise the output to the power 16.
This would indeed be the case if I would have used
sqrt(16)^16 # 4.294967296e9
On the other hand, I can imagine, that Julia replaces
x^(0.5) under the hood. However,
^ acts right associative, so then I would expect something along the line
√(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:
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:
√(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?