{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Debugging"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A crucial concept that readers have hopefully started to pick up at this point is *debugging*. Simply put, this is the process by which one fixes code until it accomplishes what it is expected to accomplish. In practice, this can feel like being a detective in a crime movie where you are also the murderer.\n",
"\n",
"There are three types of errors to debug:\n",
"\n",
"* syntax errors\n",
" * missing parentheses\n",
"* runtime errors\n",
" * overflow error\n",
"* semantic errors\n",
" * \"this answer is wrong\"\n",
" \n",
"We will explore these one at a time below."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Syntax errors\n",
"Syntax errors are errors that prevent a compiler or interpreter from understanding the source code and generating a working program.\n",
"\n",
"They are often due to typos, and are the easiest of the errors to fix. Thankfully, they appear less frequently as you master a specific programming language, although it is unreasonably to expect them to go away entirely. \n",
"\n",
"Modular programming (or breaking down the functionality of a larger program into independent and potentially interchangeable modules) helps isolate errors. Throughout this course, this means writing small and readable functions that accomplish only one task, separated into different cells of a Jupyter notebook.\n",
"\n",
"Let's see some syntax error examples."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "LoadError",
"evalue": "syntax: extra token \"mySqrt\" after end of expression",
"output_type": "error",
"traceback": [
"syntax: extra token \"mySqrt\" after end of expression",
"",
"Stacktrace:",
" [1] top-level scope",
" @ In[1]:1"
]
}
],
"source": [
"fnction mySqrt(a)\n",
" return sqrt(a)\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we see that the stack trace tells us the error is in line 1 of `In[1]`. Specifically the error is 'an extra token \"mySqrt\" after end of expression'. Putting these two clues together, we should see that the word \"function\" on the first line was incorrectly spelled."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"ename": "LoadError",
"evalue": "syntax: missing comma or ) in argument list",
"output_type": "error",
"traceback": [
"syntax: missing comma or ) in argument list",
"",
"Stacktrace:",
" [1] top-level scope",
" @ In[2]:5"
]
}
],
"source": [
"function lots_of_math(n)\n",
" var = 0\n",
" for i = 1:n\n",
" var += sqrt(2*pi*i)*(((i-1)+(i+2))/((n+5)*(5*i))\n",
" end\n",
" return var\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Another syntax error that is quite common (especially if you're writing complicated mathematical expressions): an uneven amount of parentheses. The stack trace tells us that the error is on line 5 of the above cell, but if we look around line 5, we see that it must be an error carried over from line 4 with the complicated expression.\n",
"\n",
"Here is a helpful tip to zoom in on this issue further. We can track the expression character by character from left to right, counting the number of open parentheses at each character.\n",
"\n",
"```julia\n",
"sqrt(2*pi*i)*(((i-1)+(i+2))/((n+5)*(5*i))\n",
"\n",
" 1 0 123 2 3 21 23 2 3 21\n",
"```\n",
"This count confirms that the syntax error will be solved with an extra `)` closing the second half of the expression. The extra `)` will ensure our parenthesis count returns to 0."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"ename": "LoadError",
"evalue": "syntax: unexpected \"=\"",
"output_type": "error",
"traceback": [
"syntax: unexpected \"=\"",
"",
"Stacktrace:",
" [1] top-level scope",
" @ In[3]:4"
]
}
],
"source": [
"function more_math()\n",
" i = 10\n",
" var1\n",
" = sqrt(2*pi*i)*(((i-1)+(i+2))/((i+5)*(5*i)))\n",
" println(var1)\n",
" \n",
" i = 20\n",
" var2 = \n",
" sqrt(2*pi*i)*(((i-1)+(i+2))/\n",
" ((i+5)*(5*i)))\n",
" println(var2)\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Julia is not expecting line 4 to start with an \"=\". This is not allowed and is fixed by combining line 3 and 4 into one line. However, note that you are allowed to end a line with an \"=\" as on line 8.\n",
"\n",
"When debugging, it is helpful to turn on line numbers by selecting View -> Toggle Line Numbers in a Jupyter notebook."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "LoadError",
"evalue": "syntax: line break in \":\" expression",
"output_type": "error",
"traceback": [
"syntax: line break in \":\" expression",
"",
"Stacktrace:",
" [1] top-level scope",
" @ In[4]:4"
]
}
],
"source": [
"function array_init(n)\n",
" array = Array{Float64,2}(n,n)\n",
" for i = 1:size(array,1):\n",
" for j = 1:size(array,2)\n",
" array[i,j] = i*j\n",
" end\n",
" end\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A final syntax error to explore. Julia reports that line 4 cannot have a line break in a vectorized expression using \":\". If we explore just around that line number, we'll see that again the issue was carried over from the previous line. There is an extra `:` after `size(array,1)`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Runtime errors\n",
"\n",
"Runtime errors are errors that take place while executing a program. \n",
"\n",
"These errors can sometimes be caused by typos, but are often more complex than syntax errors. You should fully read the error message(s) before attempting to fix them. If you are unsure how to interpret an error message you have never seen before, please google the exact expression reported in the error message. You are certainly not the first person to cause this error, and there may be a post online from another user who has already solved your exact issue (such as on StackOverflow.com or discourse.julialang.org).\n",
"\n",
"As always, modular programming will help isolate errors. If you have tested other parts of your program and are confident everything else is correct, then the issue is reduced to somewhere only in a few lines of code. "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"ename": "LoadError",
"evalue": "OverflowError: 25 is too large to look up in the table; consider using `factorial(big(25))` instead",
"output_type": "error",
"traceback": [
"OverflowError: 25 is too large to look up in the table; consider using `factorial(big(25))` instead",
"",
"Stacktrace:",
" [1] factorial_lookup",
" @ ./combinatorics.jl:19 [inlined]",
" [2] factorial",
" @ ./combinatorics.jl:27 [inlined]",
" [3] taylorExpansion(x::Float64, n::Int64)",
" @ Main ./In[5]:5",
" [4] top-level scope",
" @ In[5]:10"
]
}
],
"source": [
"# Compute the first n terms of the Taylor expansion of e^x centered around x=0\n",
"function taylorExpansion(x, n)\n",
" sum = 0\n",
" for i = 1:n\n",
" sum += x^n/factorial(n)\n",
" end\n",
" return sum\n",
"end\n",
"\n",
"taylorExpansion(0.5, 25)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"On line 5 of this cell, Julia is reporting that `taylorExpansion()` is calling `factorial()` in a separate Julia file which tries to lookup $25!$, but is instead returning an overflow error. This is because $25! \\approx 10^{25}$ and indeed is much too large to store in the variable `sum`. A closer inspection of this algorithm shows that we are dividing two large numbers, $x^n$ and $n!$, and the algorithm can be rewritten so as to avoid computing such large numbers explicitly, avoiding the overflow error entirely. This process was explored in [Section 1.4: For-Loops](for-loops)."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"ename": "LoadError",
"evalue": "MethodError: objects of type Float64 are not callable\nMaybe you forgot to use an operator such as \u001b[36m*, ^, %, / etc. \u001b[39m?",
"output_type": "error",
"traceback": [
"MethodError: objects of type Float64 are not callable\nMaybe you forgot to use an operator such as \u001b[36m*, ^, %, / etc. \u001b[39m?",
"",
"Stacktrace:",
" [1] lots_of_math2(n::Int64)",
" @ Main ./In[6]:5",
" [2] top-level scope",
" @ In[6]:10"
]
}
],
"source": [
"# Confuse the audience with a compicated math expression\n",
"function lots_of_math2(n)\n",
" var = 0\n",
" for i = 1:n\n",
" var += sqrt(2*pi*i)(((i-1)+(i+2))/((n+5)*(5*i)))\n",
" end\n",
" return var\n",
"end\n",
"\n",
"lots_of_math2(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Julia's error message here has dramatically improved in recent versions. While it reports this as a method error \"objects of type `Float64` are not callable,\" it also explains that this can be caused by forgetting an operator such as `*`. Indeed, that's exactly the issue. Comparing this function to `lots_of_math1()` above, we see the issue is a missing multiplication sign between `sqrt(2*pi*i)` and the other expression.\n",
"\n",
"The reason it says this is a method error is because Julia is interpreting line 5 exactly like:\n",
"```julia\n",
"var1 = sqrt(2*pi*i)\n",
"var2 = ((i-1)+(i+2))/((n+5)*(5*i))\n",
"var += var1(var2)\n",
"```\n",
"That is to say, Julia is trying to call the function `var1()` with input `var2`. This clearly does not make sense since `var1` is a floating point number, not a function, hence the error message that `Float64`s are not callable."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"ename": "LoadError",
"evalue": "UndefVarError: `relation` not defined",
"output_type": "error",
"traceback": [
"UndefVarError: `relation` not defined",
"",
"Stacktrace:",
" [1] compare(x::Int64, y::Int64)",
" @ Main ./In[7]:8",
" [2] top-level scope",
" @ In[7]:11"
]
}
],
"source": [
"# Print out a comparison between x and y\n",
"function compare(x,y)\n",
" if x < y\n",
" relation = \"less than\"\n",
" elseif x > y\n",
" relation = \"greater than\"\n",
" end\n",
" println(x, \" is \", relation, \" \", y)\n",
"end\n",
"\n",
"compare(5,5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Apparently, `relation` is not defined on line 8 above. But isn't this variable defined on line 4 or 6 directly above? Well actually not at all. Since we are calling `compare()` with inputs that satisfy $x=y$, neither conditional is evaluated as true and so `relation` is still undefined on line 8. This error can be fixed by adding a third condition for when `x==y` that sets `relation = \"equal to\"`."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"ename": "LoadError",
"evalue": "BoundsError: attempt to access 5×4 Matrix{Float64} at index [1, 5]",
"output_type": "error",
"traceback": [
"BoundsError: attempt to access 5×4 Matrix{Float64} at index [1, 5]",
"",
"Stacktrace:",
" [1] setindex!",
" @ ./array.jl:971 [inlined]",
" [2] array_init2(m::Int64, n::Int64)",
" @ Main ./In[8]:6",
" [3] top-level scope",
" @ In[8]:12"
]
}
],
"source": [
"# Initialize a 2d array to hold first m x n integer products\n",
"function array_init2(m,n)\n",
" array = Matrix{Float64}(undef,m,n)\n",
" for i = 1:n\n",
" for j = 1:m\n",
" array[i,j] = i*j\n",
" end\n",
" end\n",
" return array\n",
"end\n",
"\n",
"array_init2(5,4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A classic out-of-bounds error. You will most likely create this error several times throughout this course, and certainly many more times beyond the course! \n",
"\n",
"To debug this error, we first see that the out-of-bounds error occurs on line 6 where we are accessing `array` at `[i,j]`. Therefore, we can quickly determine that either `i` and `j` are not correct or the overall size of `array` is not correct. At this point, we would have to turn on our brains and actually start figuring out the underlying issue. The matrix is in $\\mathbb{R}^{m \\times n}$, where $m$ is the number of columns and $n$ is the number of rows - that makes sense. But we are looping our `i` counter over the first `n` columns and the `j` counter over the first `m` rows. There lies the contradiction. Correct code would swap the `i` and `j` variables:\n",
"\n",
"```julia\n",
"for j = 1:n\n",
" for i = 1:m\n",
" array[i,j] = i*j\n",
" end\n",
"end\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's explore one last runtime error example."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"ename": "LoadError",
"evalue": "MethodError: no method matching /(::String, ::Int64)\n\n\u001b[0mClosest candidates are:\n\u001b[0m /(\u001b[91m::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}\u001b[39m, ::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8})\n\u001b[0m\u001b[90m @\u001b[39m \u001b[90mBase\u001b[39m \u001b[90m\u001b[4mint.jl:97\u001b[24m\u001b[39m\n\u001b[0m /(\u001b[91m::StridedArray{P}\u001b[39m, ::Real) where P<:Dates.Period\n\u001b[0m\u001b[90m @\u001b[39m \u001b[36mDates\u001b[39m \u001b[90m/usr/local/julia-1.9.2/share/julia/stdlib/v1.9/Dates/src/\u001b[39m\u001b[90m\u001b[4mdeprecated.jl:44\u001b[24m\u001b[39m\n\u001b[0m /(\u001b[91m::Union{SparseArrays.AbstractCompressedVector{Tv, Ti}, SubArray{Tv, 1, <:SparseArrays.AbstractSparseMatrixCSC{Tv, Ti}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, false}, SubArray{Tv, 1, <:SparseArrays.AbstractSparseVector{Tv, Ti}, Tuple{Base.Slice{Base.OneTo{Int64}}}, false}} where {Tv, Ti}\u001b[39m, ::Number)\n\u001b[0m\u001b[90m @\u001b[39m \u001b[32mSparseArrays\u001b[39m \u001b[90m/usr/local/julia-1.9.2/share/julia/stdlib/v1.9/SparseArrays/src/\u001b[39m\u001b[90m\u001b[4msparsevector.jl:1654\u001b[24m\u001b[39m\n\u001b[0m ...\n",
"output_type": "error",
"traceback": [
"MethodError: no method matching /(::String, ::Int64)\n\n\u001b[0mClosest candidates are:\n\u001b[0m /(\u001b[91m::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}\u001b[39m, ::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8})\n\u001b[0m\u001b[90m @\u001b[39m \u001b[90mBase\u001b[39m \u001b[90m\u001b[4mint.jl:97\u001b[24m\u001b[39m\n\u001b[0m /(\u001b[91m::StridedArray{P}\u001b[39m, ::Real) where P<:Dates.Period\n\u001b[0m\u001b[90m @\u001b[39m \u001b[36mDates\u001b[39m \u001b[90m/usr/local/julia-1.9.2/share/julia/stdlib/v1.9/Dates/src/\u001b[39m\u001b[90m\u001b[4mdeprecated.jl:44\u001b[24m\u001b[39m\n\u001b[0m /(\u001b[91m::Union{SparseArrays.AbstractCompressedVector{Tv, Ti}, SubArray{Tv, 1, <:SparseArrays.AbstractSparseMatrixCSC{Tv, Ti}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, false}, SubArray{Tv, 1, <:SparseArrays.AbstractSparseVector{Tv, Ti}, Tuple{Base.Slice{Base.OneTo{Int64}}}, false}} where {Tv, Ti}\u001b[39m, ::Number)\n\u001b[0m\u001b[90m @\u001b[39m \u001b[32mSparseArrays\u001b[39m \u001b[90m/usr/local/julia-1.9.2/share/julia/stdlib/v1.9/SparseArrays/src/\u001b[39m\u001b[90m\u001b[4msparsevector.jl:1654\u001b[24m\u001b[39m\n\u001b[0m ...\n",
"",
"Stacktrace:",
" [1] divide_by_zero(input::String)",
" @ Main ./In[9]:3",
" [2] top-level scope",
" @ In[9]:6"
]
}
],
"source": [
"# Divide an input number by 0\n",
"function divide_by_zero(input)\n",
" return input/0\n",
"end\n",
"\n",
"divide_by_zero(\"string\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This error is somewhat unique to Julia because most other programming languages would report dividing a string by the integer zero as a syntax error. However, Julia specifically allows you to write such general code, but will only catch the error when you try and run it. There are many benefits to this approach, but for now let's just explore this one possible error. We see that the error reports that on line 7 above, we are calling `divide_by_zero()` which tries to compute `String/Int64`, something that has not been programmed.\n",
"\n",
"This error can be fixed by writing that functionality ourselves using the tools we will explore in [Section 11: Structs and Objects](structs), or by not trying to divide a string by zero in the first place! This can be enforced by rewriting the function as follows:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"ename": "LoadError",
"evalue": "MethodError: no method matching /(::String, ::Int64)\n\n\u001b[0mClosest candidates are:\n\u001b[0m /(\u001b[91m::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}\u001b[39m, ::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8})\n\u001b[0m\u001b[90m @\u001b[39m \u001b[90mBase\u001b[39m \u001b[90m\u001b[4mint.jl:97\u001b[24m\u001b[39m\n\u001b[0m /(\u001b[91m::StridedArray{P}\u001b[39m, ::Real) where P<:Dates.Period\n\u001b[0m\u001b[90m @\u001b[39m \u001b[36mDates\u001b[39m \u001b[90m/usr/local/julia-1.9.2/share/julia/stdlib/v1.9/Dates/src/\u001b[39m\u001b[90m\u001b[4mdeprecated.jl:44\u001b[24m\u001b[39m\n\u001b[0m /(\u001b[91m::Union{SparseArrays.AbstractCompressedVector{Tv, Ti}, SubArray{Tv, 1, <:SparseArrays.AbstractSparseMatrixCSC{Tv, Ti}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, false}, SubArray{Tv, 1, <:SparseArrays.AbstractSparseVector{Tv, Ti}, Tuple{Base.Slice{Base.OneTo{Int64}}}, false}} where {Tv, Ti}\u001b[39m, ::Number)\n\u001b[0m\u001b[90m @\u001b[39m \u001b[32mSparseArrays\u001b[39m \u001b[90m/usr/local/julia-1.9.2/share/julia/stdlib/v1.9/SparseArrays/src/\u001b[39m\u001b[90m\u001b[4msparsevector.jl:1654\u001b[24m\u001b[39m\n\u001b[0m ...\n",
"output_type": "error",
"traceback": [
"MethodError: no method matching /(::String, ::Int64)\n\n\u001b[0mClosest candidates are:\n\u001b[0m /(\u001b[91m::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}\u001b[39m, ::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8})\n\u001b[0m\u001b[90m @\u001b[39m \u001b[90mBase\u001b[39m \u001b[90m\u001b[4mint.jl:97\u001b[24m\u001b[39m\n\u001b[0m /(\u001b[91m::StridedArray{P}\u001b[39m, ::Real) where P<:Dates.Period\n\u001b[0m\u001b[90m @\u001b[39m \u001b[36mDates\u001b[39m \u001b[90m/usr/local/julia-1.9.2/share/julia/stdlib/v1.9/Dates/src/\u001b[39m\u001b[90m\u001b[4mdeprecated.jl:44\u001b[24m\u001b[39m\n\u001b[0m /(\u001b[91m::Union{SparseArrays.AbstractCompressedVector{Tv, Ti}, SubArray{Tv, 1, <:SparseArrays.AbstractSparseMatrixCSC{Tv, Ti}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, false}, SubArray{Tv, 1, <:SparseArrays.AbstractSparseVector{Tv, Ti}, Tuple{Base.Slice{Base.OneTo{Int64}}}, false}} where {Tv, Ti}\u001b[39m, ::Number)\n\u001b[0m\u001b[90m @\u001b[39m \u001b[32mSparseArrays\u001b[39m \u001b[90m/usr/local/julia-1.9.2/share/julia/stdlib/v1.9/SparseArrays/src/\u001b[39m\u001b[90m\u001b[4msparsevector.jl:1654\u001b[24m\u001b[39m\n\u001b[0m ...\n",
"",
"Stacktrace:",
" [1] divide_by_zero(input::String)",
" @ Main ./In[9]:3",
" [2] top-level scope",
" @ In[10]:6"
]
}
],
"source": [
"# Divide an input number by 0\n",
"function divide_by_zero(input::Number) # Only allows an input of type Number\n",
" return input/0\n",
"end\n",
"\n",
"divide_by_zero(\"string\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now the error message makes more sense. There correctly is no function `divide_by_zero()` that takes in a `String` as the input.\n",
"\n",
"Note: This fix will not take effect until after you restart the Jupyter kernel so that Julia forgets about the previous function `divide_by_zero()` which allowed `input` to be any type."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Semantic errors\n",
"\n",
"Semantic errors technically are not errors at all. If there are semantic errors in your program, the computer does not generate any error messages, but the program simply does the wrong thing.\n",
"\n",
"A classic example of a semantic error is an infinite loop or infinite recursion. There may not be an error message reported, but an infinite loop can never be intended design.\n",
"\n",
"Debugging semantic errors might take up the majority of your time while programming, so it is important to set yourself up for success in expectation of these errors. Especially as you work on larger programs and projects, start with small test cases to confirm the logic of your program makes sense. Not to sound like a broken record, but modular programming will certainly help isolate the issues; if you have already tested most of your code successfully and can rely upon it, then you only have to struggle to debug the smaller pieces that fail the tests. \n",
"\n",
"While debugging, use `println` statements to see what is really going on. A simple tip that goes a long way: assume nothing is correct and slowly start to confirm that what you think is going on inside the program is actually going on by looking at a report of the variables. A more effective method than using print statements is to use a debugger. The IDE Atom allows you debug your code carefully, but Jupyter notebooks unfortunately do not play well with debuggers, so we won't discuss them too much in this course."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array_sum (generic function with 1 method)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Returns the sum of the first n integers\n",
"function array_sum(n)\n",
" # Initialization\n",
" array = Vector{Float64}(undef,n)\n",
" for i = 1:n-1\n",
" array[i] = i\n",
" end\n",
" \n",
" # Summation\n",
" sum = 0\n",
" for i = 1:n\n",
" sum += array[i]\n",
" end\n",
" return sum\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First try to debug the above algorithm yourself. What tests would you run? Think small at first! What variables would you like to print in order to better understand what is going on under the hood? Is there a more modular way to rewrite this algorithm in order to easier test the different parts?"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# Testing cell for array_sum()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Personally, I would write debugging code as follows:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[6.9310064190874e-310]\n",
"Sum before = 0\n",
"Sum after = 6.9310064190874e-310\n",
"[1.0, 6.93086265245077e-310]\n",
"Sum before = 0\n",
"Sum after = 1.0\n",
"[1.0, 2.0, 3.0, 4.0, 6.9310026199012e-310]\n",
"Sum before = 0\n",
"Sum after = 10.0\n"
]
}
],
"source": [
"# Returns the sum of the first n integers\n",
"function array_sum(n)\n",
" # Initialization\n",
" array = Vector{Float64}(undef,n)\n",
" for i = 1:n-1\n",
" array[i] = i\n",
" end\n",
" println(array) # TODO: Debugging\n",
" \n",
" # Summation\n",
" sum = 0\n",
" println(\"Sum before = \", sum) # TODO: Debugging\n",
" for i = 1:n\n",
" sum += array[i]\n",
" end\n",
" println(\"Sum after = \", sum) # TODO: Debugging\n",
" return sum\n",
"end\n",
"\n",
"array_sum(1);\n",
"array_sum(2);\n",
"array_sum(5);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Equipped with all this information, it becomes much more simple to debug the algorithm. We see that there is an issue with initializing the array: the last entry of the array remains undefined even after initialization! That information directly leads us to fix the bounds on the first loop from `1:n-1` to `1:n`. Re-running the tests yields the expected answer, and so we can remove the debugging printouts, comfortable that the algorithm is correct."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.9.2",
"language": "julia",
"name": "julia-1.9"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}