I thought it would be neat to create a place where people could post small functions that can do useful things and possibly help other people's projects.
Here is a function I created that could help create game clocks:
gameClocks = {}
function clock(name, time)
index = 0
for i = 1,#gameClocks do
if gameClocks[i][1] == tostring(name) then
index = i
end
end
if index == 0 then
local clock = {tostring(name), num = 0}
table.insert(gameClocks, clock)
elseif index ~= 0 then
gameClocks[index].num = gameClocks[index].num + 1
if gameClocks[index].num >= time then gameClocks[index].num = 0 return true end
end
end
Here is an example for it:
l = 0
z = 0
function love.update(dt)
if clock("LClock", 20) == true then
l = l + 1
end
if clock("ZClock", 1) == true then
z = z + 1
end
love.graphics.print(tostring(z) .. " : " .. tostring(l))
end
Last edited by ZBoyer1000 on Sat Apr 16, 2016 5:46 pm, edited 1 time in total.
function clamp(number, maxvalue, minvalue)
if number > maxvalue then
number = maxvalue
elseif number < minvalue then
number = minvalue
end
return number
end
Usage: Self explanatory
Don't check my github! It contains thousands of lines of spaghetti code in many different languages cool software! https://github.com/Sulunia
Lua threats each number as a floating-point number. By definitions floating-point numbers are approximated and operations need to be taken with care in order to avoid (or at least to keep low) error propagation. By dismissing the additional multiplication we let the error propagate less quickly (in general multiplications propagates errors more quickly that additions, especially when numbers have big differences in the exponent).
Sulunia wrote:Math optimization?
To be honest, this should be called "peephole optimization", since we are only considering a small part of a source-code (a single formula) and reorganize it by reducing the amount of math operations involved. Your version features an additional multiplication that we can get rid of (and remember that in general, and especially on embedded devices, multiplication is by far the most costly operation).
We had another thread with a lot of small functions in it if you want to have a peak through that: viewtopic.php?f=4&t=77599
As long as we don't start the lua equivalent of NPM's leftpad disaster, I'm all for these kinds of threads. It's a good resource for newbies to learn how different pieces of code would be written.
The version that you've written may go past b even when rate is still < 1. See examples in the above link.
Basically, a*(1-rate) + b*rate guarantees that when rate = 0, the result is exactly a, and when rate = 1, the result is exactly b. The experiments show that it's also monotonic (edit: even if no one was able to give proof).
On the other hand, a+b-a does not always return b.
t = 0.9999999999999999
A = -0.3465728856142666
B = 0.653769243987566
assert(t < 1)
assert(A*(1-t)+B*t <= B, "A*(1-t)+B*t is > B even if t < 1")
assert(A+(B-A)*t <= B, "A+(B-A)*t is > B even if t < 1")
--[[
Error: main.lua:6: A+(B-A)*t is > B even if t < 1
stack traceback:
[C]: in function 'assert'
main.lua:5: in main chunk
[C]: in function 'require'
[string "boot.lua"]:428: in function <[string "boot.lua"]:274>
[C]: in function 'xpcall'
--]]
Update: Comparing single-precision with double-precision results using both methods yielded the exact same maximum and minimum errors in the exact same t values. Test program: http://www.formauri.es/personal/pgimeno/pastes/lerp.c