In [1]:
using Dates
include("printmat.jl")
Out[1]:
Julia has many different types of variables: signed integers (like 2 or -5), floating point numbers (2.0 and -5.1), bools (false/true), bitarrays (similar to bools, but with more efficient use of memory), strings ("hello"), Dates (2017-04-23) and many more.
The numerical types also comes with subtypes for different precisions, for instance, Float16, Float32 and Float64. Unless you specify otherwise, code like
a = 2
b = 2.0
gives an Int64 and a Float64 respectively (at least on the 64 bit version of Julia).
In [2]:
a = 2 #integer, Int (Int64 on most machines)
b = 2.0 #floating point, (Float64 on most machines)
A = [1;2]
B = [1.0;2.0]
println("a: ",typeof(a))
println(a)
println("\nb: ",typeof(b))
println(b)
println("\nA: ",typeof(A))
printmat(A)
println("B: ",typeof(B))
printmat(B)
In [3]:
x = [1,10,100,1000]
#println(x[3.0]) #uncomment and run. Will give an error
println(x[3])
In [4]:
c = 2 > 1.1
println("c: ",typeof(c))
println(c)
C = A .> 1.5
println("\nC: ",typeof(C))
printmat(C)
println("A BitArray is a more economical array version of Bool, but prints as 0/1 by printmat.\n",
"Notice that typeof(C[1]) gives: ",typeof(C[1]))
In [5]:
t = 'a' #Char, just one letter
println(typeof(t))
txt = "Dogs are nicer than cats." #String, could be a long novel
println(typeof(txt))
A calculation like "integer" + "float" works and the type of the result is a float (the more flexible type). Similarly, "bool" + "integer" will give an integer.
There are also direct ways of converting a variable from one type to another using the convert() function.
In [6]:
println("Int + Float64: ",1+2.0)
println("Bool + Int: ",(1 .> 0) + 2)
In [7]:
x = [1.1;10.1;100.1]
println("x: ",typeof(x))
printmat(x)
B_to_Int = round.(Int,x) #Float64 -> Int by rounding
println("rounding x to Int: ",typeof(B_to_Int))
printmat(B_to_Int)
A = [1;2]
println("A: ",typeof(A))
printmat(A)
A_to_Float64 = convert.(Float64,A) #Int -> Float64
println("after converting A to Float64: ",typeof(A_to_Float64))
printmat(A_to_Float64) #Float64.(A) also works
In [8]:
C = A .> 1.5
C_to_Int = convert.(Int,C) #BitArray -> Int
println(typeof(C_to_Int)) #Int.(C) also works
printmat(C_to_Int)
D = [1;0;1]
D_to_Bool = convert.(Bool,D) #Int -> BitArray
println(typeof(D_to_Bool)) #Bool.(D) also works
printmat(D_to_Bool)
In [9]:
println(false*NaN)
println(false*Inf)
println(convert(Int,false)*NaN)
println(convert(Int,false)*Inf)
In [10]:
x = 1.2
z = [1.2,1.3]
println("$x is a Number: ",isa(x,Number))
println("$x is an Int: ",isa(x,Int))
println("$x is an Int or a Float64: ",isa(x,Union{Int,Float64}))
println("$z is a Float64: ",isa(z,Float64))
println("$z is an Array: ",isa(z,Array))
In [ ]: