3 ms·
I like Julia a lot, and thought I understood some of it, but this article puzzles me: > This output is saying that a floating point multiplication operation is
by FabHK 8y ago
I like Julia a lot, and thought I understood some of it, but this article puzzles me:
> This output is saying that a floating point multiplication operation is performed and the answer is returned.
But "this output" is:
%2 = mul i64 %1, %0
ret i64 %2
which looks very much like an int64 operation and return.
Similarly:
> Here we get an error. In order to guarantee to the compiler that ^ will give an Int64 back, it has to throw an error. If you do this in MATLAB, Python, or R, it will not throw an error.
while the quoted input and output is
In [6]: 2^-5
Out[6]: 0.03125
(ie, no error, but the correct (floating point) result.)
- ChrisRackauckas 8y agoThis was written back in Julia v0.5, and the compiler got smarter so I need to update my examples :). Here, Julia specializes now on the fact that `-5` is a literal, and then inlines the literal and corrects the output type using that value. If you define it as a variable and stop constant propogation, it'll error. Stuff like this are making it harder to write tutorials to show what's actually going on, because literals and constants are all getting optimized on now! Another confounding fact is that Julia optimizes on small unions, so the generated code isn't that bad anymore. Now it just creates a branch. It used to have to do all of inference and dynamic dispatching on the fly, which is what it has to do in fully uninferrable code of course, but a union of two things just does a type check and splits. So now... that example is not as bad as it used to be...
- FabHK 8y agoAh, well, I guess that's good news then!
- krull10 8y agoAs a rule of thumb, it is probably a good idea to ignore Julia articles that are more than a year or so old. Anything that is discussing pre-1.0 may be completely irrelevant at this point. The language has evolved a lot, even the past few years, and it is only with the 1.0 release last fall that the devs promised to start maintaining backwards compatibility. So there are lots of older articles discussing drawbacks to Julia, or ways to do things in Julia, that simply aren’t relevant anymore. Unfortunately they still often are the first links that come up when searching google for Julia questions.
- kgwgk 8y ago> In [6]: 2^-5 > Out[6]: 0.03125 > (ie, no error, but the correct (floating point) result.) On the other hand: julia> 16^17 0
- ChrisRackauckas 8y agoThat's just integer overflow.
- kgwgk 8y agoI know. But I don't think that's the correct (floating point) result most people would expect, so it's worth signaling. julia> 2^62 4611686018427387904 julia> 2^63 -9223372036854775808 julia> 2^64 0
- FabHK 8y agoTo be fair, though, this behaviour (namely, staying close to machine arithmetic) has been discussed and decided upon, and advertised. It's what enables the speed. C/C++ makes the same choices, by and large.
- kgwgk 8y agoI don't think C/C++ have integer exponentiation so even C/C++ programmers may be surprised here. I don't know if the behaviour of the power operator when both numbers are integers (and depending on whether the exponent is negative or positive) is mentioned in the documentation.
- krull10 8y agoThis is something that was just heavily discussed on the Julia Discourse, with a big back and forth between individuals used to the computer integer arithmetic rules vs people wanting Python like functionality. Based on the confusion there, it definitely seems that it could be made clearer in the docs that if one wants floating point results one needs to use 10.0^17 instead of 10^17.