hi
there is a non stable result in power operator. this is the example. when I call the power with the variable the power not works. but when calling directly it
julia> a = -1.9345e6
-1.9345e6
julia> a^1.5
ERROR: DomainError with -1.9345e6:
Exponentiation yielding a complex result requires a complex argument.
Replace x^y with (x+0im)^y, Complex(x)^y, or similar.
Stacktrace:
[1] throw_exp_domainerror(::Float64) at .\math.jl:35
[2] ^(::Float64, ::Float64) at .\math.jl:782
[3] top-level scope at none:0
The - operator has lower precedence than the ^ operator, so the expression -1.9345e6^1.5 means -(1.9345e6^1.5) rather than (-1.9345e6)^1.5. If you try the a = -1.9345e6; a^1.5 version in Matlab you’ll get the same imaginary value as complex(a)^1.5 gives in Julia.
While preparing an introductory talk about Julia on my University, I am looking and Chris Rackauckas’s notes why Julia. I am puzzled with his example with powers, as 5^5 should return an Int (and it does), whereas 5^-5 should fail. Nevertheless 5^-5 executes correctly in repl, but when I define function
note the literal_pow (which indeed comes from that PR), while in the function it is not constant, so is not parsed/lowered like that, but as an application of ^:
Thanks @mauro3 and @Tamas_Papp. I do not want to achieve anything special, I am just interested in internals, how does it work. Your explanation is great, yet it adds another thing I do not understand well.
Personally, I would prefer that users type inv(a)^n if that’s what they want, but that adds yet another item to the list of unexpected things that surprises people who started using Julia without bothering to read the manual.
In exchange for resolving that, the confusion is amplified and backshifted in time to the point where users become interested in finer points of the language, and encounter the magic that was used in the implementation.
That remark was not targeted at you (but of course, re-reading the manual from time to time is great, a lot of effort goes into it and it and it is really useful), it was more about the reasons that I assume lead to the introduction of this feature.
Namely, the pivotal role of type stability in Julia is very easy to miss if one just skims through a short tutorial or similar. Yet it explains the design choices that answer questions like “why doesn’t √-2 work?”.
I am unsure whether this should be a new thread, the title of this thread fits well…
Typing 16^16 in the REPL gives a result of 0 (Julia v1.4).
I guess this is related to the size of Int64 (and 2^64 logically gives the same answer, so does 16^17). But the fact that no error is raised here may be a problem, as one may take long to find out where the error comes from (in a larger calculation).
Integer overflow is a different issue which comes up regularly. The most recent discussion about it is here, including reasons for this behaviour, ways around it and some suggestions to make it easier on newcomers.