Exercise Solutions: The Basic Data Types

This section contains solutions to the end-of-section exercises found in this book for the Learn Julia chapter.

1.1 Numbers

Exercise 1.1.1: What are the type minimum and maximum for Float64?

# SOLUTION:
typemin(Float64), typemax(Float64)
(-Inf, Inf)

Exercise 1.1.2: Encode true as a Float64.

# SOLUTION:
Float64(true)
1.0

Exercise 1.1.3: Use Julia to calculate $\lfloor 10/3 \rfloor \mod 2$.

# SOLUTION:
(10 ÷ 3) % 2
1

Exercise 1.1.4: What is the output of the following logical expression (recommended that you don’t use Julia):

(((true  (1  2)) || (1 >= 1)) && (true && (1 != 2) && (2  2)))  false
# SOLUTION:
(((true  (1  2)) || (1 >= 1)) && (true && (1 != 2) && (2  2)))  false
false

1.2 Arrays

Exercise 1.2.1: Create an array called y with values 2, 10, 3, 44, and 70 encoded as Float32s.

y = Float32[2, 10, 3, 44, 70]
5-element Array{Float32,1}:
  2.0
 10.0
  3.0
 44.0
 70.0

Exercise 1.2.2: Halve each element of y. Do not edit the original array.

y ./ 2
5-element Array{Float32,1}:
  1.0
  5.0
  1.5
 22.0
 35.0

Exercise 1.2.3: Reassign y to an array of boolean values indicating whether or not each element is greater than or equal to 43.

y = y .≥ 43
5-element BitArray{1}:
 false
 false
 false
  true
  true

Exercise 1.2.4: Copy the anonymous function definition below and run it.

not = x -> !(x)

Use this function to negate the values in y.

not = x -> !(x)
not.(y)
5-element BitArray{1}:
  true
  true
  true
 false
 false