Numbers and Math

This page provides syntax for using numbers and mathematic operations in Julia. Each section includes an example to demonstrate the described syntax and operations.

Types of Numbers

  • Integer (positive and negative counting number) - e.g., -3, -2, -1, 0, 1, 2, and 3

    • Signed: Int8, Int16, Int32, Int64, and Int128

    • Unsigned: UInt8, UInt16, UInt32, UInt64, and UInt128

    • Boolean: Bool (0 = False and 1 = True)

  • Float (real or floating point numbers) - e.g., -2.14, 0.0, and 3.777

    • Float16, Float32, Float64

Use typeof() function to determine type

Input:

# Define two variables x and y
x = 100
y = 3.14

# Print out the variable types for each
println(typeof(x))
println(typeof(y))

Output:

Int64
Float64

Arithmetic Operators

Input:

# Demonstrates different math operations
using Printf

n1 = 7    # First number
n2 = 3    # Second number
 
# Output results of different math operations
println("$n1 + $n2 = $(n1 + n2)")             # Addition 
println("$n1 - $n2 = $(n1 - n2)")             # Subtraction 
println("$n1 * $n2 = $(n1 * n2)")             # Multiplication 
println("$n1 / $n2 = $(n1 / n2)")             # Division 
@printf("%d / %d = %.2f\n", n1, n2, n1 / n2)  # Print to 2 decimal places
println("$n1 ^ $n2 = $(n1 ^ n2)")             # Power/Exponent
println("$n1 % $n2 = $(n1 % n2)")             # Modulo/Remainder

Output:

7 + 3 = 10
7 - 3 = 4
7 * 3 = 21
7 / 3 = 2.3333333333333335
7 / 3 = 2.33
7 ^ 3 = 343
7 % 3 = 1

Comparison Operators and Functions

Input:

# compare.jl                                                                                                 
# Demonstrate comparison operators                                                                               

# Assign values to variables using parallel assignment                                                           
c1, c2, c3, c4 = 25, 50, 75, 50
println("c1 = $(c1), c2 = $(c2), c3 = $(c3), c4 = $(c4)")

# Output results of different comparison operations                                                             
 
# Testing equality                                                                                               
println("  c1 = c3 is $(c1 == c3)")
println("  c2 = c4 is $(isequal(c2, c4))")

# Changing values using abbreviated assignment operators                                                        
c1 *= 3    	# Shorthand for c1 = c1 * 3                                                                       
c4 += 1    	# Shorthand for c4 = c4 + 1                                                                       

println("c1 = $(c1), c2 = $(c2), c3 = $(c3), c4 = $(c4)")
 
# Testing less than and greater than
println("  c1 < c2 is $(c1 < c2)")
println("  c4 <= c2 is $(c4 <= c2)")
println("  c1 > c2 is $(c1 > c2)")
println("  c3 >= c2 is $(c3 >= c2)")

Output:

c1 = 25, c2 = 50, c3 = 75, c4 = 50
  c1 = c3 is false
  c2 = c4 is true
c1 = 75, c2 = 50, c3 = 75, c4 = 51
  c1 < c2 is false
  c4 <= c2 is false
  c1 > c2 is true
  c3 >= c2 is true

Exercises

  • Create a Health Calculator Using Julia - Forthcoming!

Resources

Last updated