lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 24/06/2014 02:43, Choonster TheMage wrote:
On 24 Jun 2014 13:52, "Lucas" <cad.lua@lucasjr.nospammail.net> wrote:
Ok, my intention was to write such a function

function modulus(n, d) return n - (q = math.floor(n/d)) * d, q; end

is there any means to write it down just as simple?

r, q = modulus(10, 3)

Sticking with your existing technique:
function modulus(n, d)
     local q = math.floor(n/d)
     return n - q * d, q
end

Using the modulus operator (I'm pretty sure this is correct):
function modulus(n, d)
     return n % d, math.floor(n/d)
end
Pretty sure the internal % does n - floor(n/d) * d, which means you can save a division by using the other thing...

Regards,
Choonster