DKL9 GitList
Repositories
DKL9 home
computation_practice
Code
Commits
Branches
Tags
Search
Tree:
42c0749
Branches
Tags
master
computation_practice
main.lua
Add sine operation
John Smith
commited
42c0749
at 2022-360 17:33:26
main.lua
Blame
History
Raw
#!/usr/bin/env lua local function identity(x) return x end local function cup(x) if type(x) == "table" then return table.unpack(x) else return x end end local TYPES = { ln = { -- how to generate the problem (returns problem-object) gen = function() return math.exp(6.9 * math.random()) end, -- how to present the problem (for string.format) disp = "ln(%.2f)", -- produce correct answer-object from problem-object func = math.log, -- check quality of guess g for problem p score = function(p, g) return -(math.log(math.abs(math.log(p) - g), 10) + 1.5)^3 end, }, sin = { gen = function() return 1.5 * math.random() end, disp = "sin(%.3f)", func = math.sin, score = function(p, g) return -(math.log(math.abs(math.sin(p) - g), 10) + 1.5)^3 end }, } -- ideally, configure these parameters with options/inputs local TYPE = TYPES.sin local TIME = 30 local ts = 0 local st = os.time() local pc = 0 while os.time() - st < TIME do local rp = TYPE.gen() local ans = TYPE.func(rp) io.write(string.format(TYPE.disp .. " ? ", cup(rp))) io.flush() inp = tonumber(io.read("l")) or 0 local sc = TYPE.score(rp, inp) if sc > 0 then io.write(string.format("✓ correct (%.3f)\n", ans)) else io.write(string.format("wrong (%.3f)\n", ans)) end ts = ts + sc pc = pc + 1 end io.write(string.format("score %.1f from %d problems in %.1f seconds\n", ts, pc, TIME))