I am relatively new to Julia (<1 year) and I am developing a tool for tracking floating point addition. I have set up my work as a package, here is a reduced MWE:
module TestModule
# used for storing information when addition is done
mutable struct Tracking
lastResult::Float64
info::String
Tracking() = new(0, "default message")
end
# overloading of addition, t has default argument in case of none provided
function +(in1::Float64, in2::Float64, t::Tracking = Nothing)
+(a, b) = Base.:+(a, b)
t.info = "writing a string to t.info when addition is performed"
t.lastResult = in1 + in2
return in1 + in2
end
# run f() and track information in t
function execute(f::Function, t::Tracking)
+(a, b) = TestModule.:+(a, b, t)
f()
end
function report(t::Tracking)
println("final value = ", t.lastResult)
println(t.info)
end
end
The aim is that the user creates an instance of a Tracking
struct, then runs their program with execute()
and uses report()
for feedback. For example I have created a script for testing.
using TestModule
# create a Tracking instance
tracking = TestModule.Tracking()
# void function that performs whatever we want to do, in this case sum of 1000 rands
function myExperiment()
n = 1000
x = rand(n)
sum = 0
for k in 1:n
sum += x[k]
end
println(sum)
end
# run and report
TestModule.execute(myExperiment, tracking)
TestModule.report(tracking)
I have my TestModule
package configured, then the second file is a script in a separate file.
When I run the second file as a script, I want the fields of tracking
to have changed, however the return is instead:
505.3979219546986
final value = 0.0
default message
The values of tracking
aren’t being updated. I assume this is normal behaviour, so is there a way I can modify my code in order for the instance of my data structure to be updated as the program runs?