I find the following behavior of print statement quite strange.
Let’s say I have an array of floating point numbers and I print it. That is,
a = [2.0, 3.0]
I get the following result upon executing the code above:
However, if I do the same thing with an array of integers, I get this
b = convert.(Int32, a)
This time I get the result
Why this inconsistency? Why is it showing the datatype now?
Thank you for your help.
The unannotated array
[2, 3] is reserved for
Int64 specifically, just like how
[2.0, 3.0] is reserved for
Float64. On the other hand,
println([2.0f0, 3.0f0]) prints
That type annotation is just syntax for specifying the element type of an array literal. It’s useful to write explicitly if Julia guesses wrong. For example, writing
[1.0, 1] converts everything to
Float64 for performance reasons, but if I needed to keep the types as is, I would write