Home » Julia

Division Error in Julia

Julia DivideError() Example: Here, we are going to learn the different ways when a division error may occur in Julia programming language?
Submitted by IncludeHelp, on March 27, 2020

div() function is used to divide for the integer division, by using this function we can get exception/error at two cases,

  1. Dividing an integer by zero (Consider example 1)
  2. Dividing the lowest negative number integer by -1 (Consider example 2)

In the above two cases, a DivideError throws.

There is another exceptional case with the rem() and mod() functions when the second argument is zero (Consider example 3).

Example 1:

# Case 1: Dividing an integer by zero 

x = 10
y = 2

println("x: ", x)
println("y: ", y)

result = div(x,y)
println("result: ", result)

# assigning 0 to y
y = 0 
result = div(x,y)
println("result: ", result)

Output

x: 10
y: 2
result: 5
ERROR: LoadError: DivideError: integer division error

Example 2:

# Case 2: Dividing the lowest negative number integer by -1

x = 10
y = 2

println("x: ", x)
println("y: ", y)

result = div(x,y)
println("result: ", result)

# assigning lowest negative number integer to x
# and -1 to y
x = typemin(Int64)
y = -1

result = div(x,y)
println("result: ", result)

Output

x: 10
y: 2
result: 5
ERROR: LoadError: DivideError: integer division error

Example 3:

# Case 3: With the rem() and mod() functions 
# when the second argument is zero

x = 10
y = 3

println("x: ", x)
println("y: ", y)

println("rem(x,y): ", rem(x,y))
println("mod(x,y): ", mod(x,y))

# assigning 0 to y
y = 0

println("rem(x,y): ", rem(x,y))
println("mod(x,y): ", mod(x,y))

Output

x: 10
y: 3
rem(x,y): 1
mod(x,y): 1
ERROR: LoadError: DivideError: integer division error


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.