Basics of Julia & Numerical Computation Part 2

Basics of Julia & Numerical Computation Part 2

Data types, comments & operators

For the part 2 of Basics of Julia & Numerical Computation, we are going to talk about:

  • Data Types
  • Comments
  • Mathematical Operators
  • Boolean Operators
  • Updating Operators

Data Types

I personally categorize julia data types into 3 main broad categories:

  • Numerical data type
  • Strings data type
  • and Booleans

Numerical data type

  • Under this category, we have integers (Int) and floating-point numbers (Float).
  • They can either be signed or unsigned.
  • The chart below clearly distinguishes signed numbers from unsigned numbers.

binary_rep.jpg source: tutorialspoint.com/unsigned-and-signed-bina..

  • In summary, unsigned representation is only for positive numbers and has only one zero(0). In constrast, signed representation is for both positive and negative numbers and can have one or two zeros depending on the technique used to 'sign' the integers.

Declaring Integers & Floating Point Numbers

## Reminder
# typeof() function displays the data type of a value or variable.

num1 = 2
typeof(num1)     # outputs Int64

num2 = 3.1
typeof(num2)     # outputs Float64
  • Int and Float will default to Int64 or Int32 and Float64 or Float32 for 64-bit and 32-bit architectures respectively.
  • The type of integer or floating-point number used determines the largest value that can be represented. For example, Int64 data type can represent numbers between

    $$ -2^{63} \text{ and } 2^{31} - 1 $$

  • The full table of Integers and their limits can be found on the julia docs page here.
  • What happens if one writes a program that results to a number that cannot be represented by a chosen data type? See the example below:

Screenshot_20220621_174426.png

  • From the example above, the code (num1 + num2) generates a wrong output because the highest number represented in Int8 is 127. This is not a bug but a limitation caused by the integer type!
  • This is known as overflow behaviour in julia. It can be solved by changing the Int or Float type to a higher bit (i.e. 64 and 128).
  • According to the julia docs, overflow behaviour is best migated by using BigInt or BigFloat data type because they can represent any numerical value.

I encountered an issue with integer overflow and in future series, I will explain how BigInt solved the issue .

String Data Type

Under string data type, we have:

  • Char - For a single character. It is enclosed strictly by single quotes.
  • String - For one or more characters. It is enclosed strictly by double quotes.

Declaring Strings & Chars

# Declaring a char
letter = 'a'

# Declaring a string
word = "Hello World!"
  • If a string is declared using single quotes, julia will raise an error.

Screenshot_20220621_183308.png

Boolean Data Type

  • The Boolean values in julia are true and false.

    yes = true
    no = false
    typeof(yes)     # outputs Bool
    

The next thing we are going to cover is commenting in Julia

Commenting

  • Comments are written using the hashtag symbol (#).
  • Multiline comments are written by placing it between #= and =#.
#=
This is a 
Multiline comment
in julia
=#

Table of Operators in Julia

Math Operators

SymbolMeaning
+Addition
-Subtraction
*Multiplication
/, \Division
^Power
%Modulus
÷ (\div <tab>)Integer division
=Assignment

Boolean Operators

SymbolMeaning
!not
&&and

|| --> or

for some reason, I cannot add it to the table

Updating Operators

Symbol (with usage)Meaning
x += 1x = x + 1
x -= 1x = x - 1
x /= 1x = x / 1
x *= 1x = x * 1
x ÷= 1x = x ÷ 1
x %= 1x = x % 1
x ^= 1x = x ^ 1

Acknowledgements

  • Appreciation to julia slack community who helped me out with overflow behaviour in julia when I run into an issue.

Resources

  • Julia Academy

  • Julia Docs

  • Curiosity