Very strange language choice for printing integer vs float arrays

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]
println(a)

I get the following result upon executing the code above:

[2.0, 3.0]

However, if I do the same thing with an array of integers, I get this

b = convert.(Int32, a)
println(b)

This time I get the result

Int32[2, 3]

Why this inconsistency? Why is it showing the datatype now?

Thank you for your help.

try

b = convert.(Int, a)

1 Like

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 Float32[2.0, 3.0].

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 Real[1.0, 1].

1 Like