pic
Personal
Website

2c. Numbers

PhD in Economics

Introduction

The previous section introduced the concept of variables, distinguishing between those containing a single element (scalars) and collections. This section expands on scalars, exclusively focusing on those holding numberic values.

Numbers

Computers store numbers in different formats, treating integers and decimal numbers as separate entities. Even within a category, multiple representations emerge, depending on the aimed level of precision. The precision of a number is determined by the number of bits allocated in memory to store it, which in turn defines the maximum range of values supported by a data type. [note] For instance, the so-called 8-bit integer is limited to representing values between –128 to 127. Likewise, a 32-bit floating-point number, which is used for decimal numbers, can represent up to 7 significant digits of precision. These features extend well beyond Julia, and are intrinsic to how computers operate at a fundamental level.

In modern computers, numeric types typically have a default precision of 64 bits. These objects are defined in Julia as:

  • Int64 for integers.

  • Float64 for decimal numbers. [note] The term "Float" stands for "Floating Point", and is how computers represent decimal numbers.

Remark
Julia provides the type Int as a more versatile option than Int64, as it adapts to your computer's architecture. Specifically, Int defaults to Int64 on 64-bit systems and Int32 on 32-bit systems. Since most modern machines operate on a 64-bit architecture, Int typically defaults to Int64. Note that there's no equivalent adaptive type Float for floating-point numbers. Instead, Julia takes Float64 as the default floating-point type.

It's worth emphasizing that Int64 and Float64 are two different data types. Thus, while 1 is a value with type Int64, the same number becomes 1.0 as a Float64 type.

Code

x = 1       # `Int64`

y = 1.0     # `Float64`
z = 1.      # alternative notation for `1.0`

Remark
To enhance code readability, you can break up long numbers by inserting underscores _.

Code

x = 1000000               
y = 1_000_000             # equivalent to `x` and more readable

x = 1000000.24            
y = 1_000_000.24          # '_' can be used with decimal numbers

The type Float64 encompasses not only decimal numbers, but also two special values: Inf for infinity, and NaN for indeterminate expressions such as 0/0 (NaN stands for "not a number"). Considering this, all the following variables are of type Float64.

Code

x = 2.5

y = 10/0

z = 0/0

Output in REPL
julia>
x
2.5

julia>
y
Inf

julia>
z
NaN

Boolean Variables

A distinct numeric type is Bool, which facilitates the representation of Boolean variables. These variables can only take on the values true and false. Internally, they're implemented as integers, with true and false respectively equivalent to 1 and 0. Due to this property, Julia accepts 1 and 0 interchangeably with true and false.

Boolean expressions arise when conditions are evaluated, such as determining whether a number exceeds a value or whether a string matches a specific pattern. These conditional evaluations yield Boolean values, which can then be employed to control the program's flow. Some examples of Boolean values are presented below.

Code

x = 2
y = 1

z = (x > y)       # is `x` greater than `y` ?
z = x > y         # alternative notation (potentially confusing, note it does not imply 'z = x')

Output in REPL
julia>
z
true

Arithmetic Operators

Numbers can be manipulated through a variety of arithmetic operators. These operators are represented by familiar symbols, akin to those commonly found in other programming languages.

Julia's Arithmetic Operator Meaning
x + y addition
x - y subtraction
x * y product
x / y division
x^y power (\(x^y\))

It's worth noting that all the operators presented above are binary. Consequently, they follow the syntax x <symbol> y, as indicated in our discussion about the syntax of operators.