In this section, we cover the basic data types in Julia and operations (incl. arithmetic and logical operators). We also cover some of Julia’s variable features, including Unicode characters.
Elementary Data Types
Julia’s elementary data types are analagous to those of other computing languages, although they differ in two big ways:
- Floating point numbers and integers can be represented with varying precision and using a different amount of memory.
- Julia recognizes individual unicode characters as a datatype.
The first of these is important because it allows you to optimize your code for memory usage. The main numerical types are
Float64. The first two represent integer values and the second two floating point decimal values. Julia defaults to the 64-bit types when no type is specified.
As you can see, we use the
typeof function to get the data type. The primitive integer types in Julia are:
|Type||Signed?||Number of bits||Smallest value||Largest value|
||✓||8||-2^7||2^7 - 1|
||8||0||2^8 - 1|
||✓||16||-2^15||2^15 - 1|
||16||0||2^16 - 1|
||✓||32||-2^31||2^31 - 1|
||32||0||2^32 - 1|
||✓||64||-2^63||2^63 - 1|
||64||0||2^64 - 1|
||✓||128||-2^127||2^127 - 1|
||128||0||2^128 - 1|
Bool objects are considered to be integer types; this is because in Julia, as in many other languages, values are eitehr truthy or falsy, and so we can represent
The maximum and minimum of each type can be found using the
The primitive floating point types in Julia are:
|Type||Precision||Number of bits|
Julia also has types for strings, characters, and arrays, but these will be covered later.
Julia contains the standard suite of operations, including
÷ (floor division),
\ (inverse division),
1 + 2, 1 - 2, 1 * 2, 1 / 2, 1 ÷ 2, 1 \ 2, 1 ^ 2, 1 % 2
(3, -1, 2, 0.5, 0, 2.0, 1, 1)
As with many other languages, variables are assigned using the assignment operator
=. Variables are case sensitive and their values are changeable.
x = 1
For all of the operators above, you can append an equal sign
= to update a value assigned to a variable name:
x += 2 x
x /= 2 x
Note the operator
÷ for integer division. This operator is an instance of Julia’s ability to include Unicode characters in code, which is useful for defining variables and for allowing more accurate mathematical notation. These characters are added using by appending a
\ to the beginning of their name and pressing TAB. Some examples:
For most characters, these are similar to their LaTeX command (if they are available in LaTeX).
ℯ # ℯ (|euler) is prefedined as Euler's constant, but the character 'e' is not
ℯ = 2.7182818284590...
π # π (\pi) is also predefined
π = 3.1415926535897...
Most characters do not have a preset value, and so you can assign them certain values.
δ = 0.5
Comparison and Logical Operators
Julia provides a suite of comparison operators to perform comparisons between data types and logical operators to create complex logical statements that evaluate to
Bool values. Because of the unicode characters, some operators have more than one representation.
||not equal to|
||greater than or equal to|
||less than or equal to|
Placing expressions on either sides of these operators yields a
false value. You can negate a boolean value using the
! (not) operator.
1 ≠ 2, 1 == 1, 1 == 2, 2 ≤ 1, 2 ≥ 1
(true, true, false, false, true)
((1 ≠ 2) || (2 ≥ 1)) && ((1 ≤ 2) ⊻ (2 != 1))
!(((1 ≠ 2) || (2 ≥ 1)) && ((1 ≤ 2) ⊻ (2 != 1)))
Exercise 1.1.1: What are the type minimum and maximum for
Exercise 1.1.2: Encode
true as a Float64.
Exercise 1.1.3: Use Julia to calculate $\lfloor 10/3 \rfloor \mod 2$.
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