44

I want to check, if a number is divisible by another number:

for i = 1, 100 do
    if i % 2 == 0 then
        print( i .. " is divisible.")
    end
end

This should work without any problems, but with the Lua in my server the script doesn't run if there is a % in the script... I dont know whats the reason, so is there any "replacement" for that? So I could check the number divsibility?

Thank you.

ninesided
  • 23,085
  • 14
  • 83
  • 107
Cyclone
  • 14,839
  • 23
  • 82
  • 114
  • 1
    What version of Lua is the server running? – Nicol Bolas Mar 14 '12 at 04:02
  • I think its 5.0 or later `:S`. – Cyclone Mar 14 '12 at 04:09
  • 6
    sounds like you have some encoding problems; maybe if you find what encoding is it, you might be able to sneak a `%` through. try '%%' or '\%' or '%25' – Javier Mar 14 '12 at 04:19
  • I am downvoting this question because I believe it is asking the wrong question: The real question here is "Why is `%` not working for me in Lua"? According to the [Lua Documentation](https://www.lua.org/manual/5.1/manual.html) *Lua supports the usual arithmetic operators: the binary + (addition), - (subtraction), * (multiplication), / (division), % (modulo), and ^ (exponentiation); and unary - (negation).* – Simon Forsberg Jan 07 '18 at 20:48
  • 1
    @Javier Lua 5.0 did not support the `%` operator. 5.1 and later however, does. See my answer. – Simon Forsberg Jan 07 '18 at 20:52

6 Answers6

58

Use math.fmod(x,y) which does what you want:

Returns the remainder of the division of x by y that rounds the quotient towards zero.

http://www.lua.org/manual/5.2/manual.html#pdf-math.fmod

Brad Parks
  • 66,836
  • 64
  • 257
  • 336
lhf
  • 70,581
  • 9
  • 108
  • 149
  • 4
    `math.fmod` did not exist in Lua 5.0, it was renamed from `math.mod` to `math.fmod` in Lua 5.1. However, Lua 5.1 also added the `%` operator, so if the OP is running Lua 5.0, `fmod` is the wrong function to use. – Simon Forsberg Jan 07 '18 at 21:01
30

It's not ideal, but according to the Lua 5.2 Reference Manual:

a % b == a - math.floor(a/b)*b

ninesided
  • 23,085
  • 14
  • 83
  • 107
7

Lua 5.0 did not support the % operator.

Lua supports the usual arithmetic operators: the binary + (addition), - (subtraction), * (multiplication), / (division), and ^ (exponentiation); and unary - (negation).

https://www.lua.org/manual/5.0/manual.html

Lua 5.1 however, does support the % operator.

Lua supports the usual arithmetic operators: the binary + (addition), - (subtraction), * (multiplication), / (division), % (modulo), and ^ (exponentiation); and unary - (negation).

https://www.lua.org/manual/5.1/manual.html

If possible, I would recommend that you upgrade. If that is not possible, use math.mod which is listed as one of the Mathematical Functions in 5.0 (It was renamed to math.fmod in Lua 5.1)

Community
  • 1
  • 1
Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
5
for i = 1, 100 do
    if (math.mod(i,2) == 0) then
        print( i .. " is divisible.")
    end
end
Macmade
  • 52,708
  • 13
  • 106
  • 123
anonymous
  • 61
  • 1
  • 1
5
function mod(a, b)
    return a - (math.floor(a/b)*b)
end
Sled
  • 18,541
  • 27
  • 119
  • 168
Jim Gao
  • 149
  • 2
  • 4
3

Use math.fmod, accroding lua manual math.mod was renamed to math.fmod in lua 5.1.

vr3C
  • 1,734
  • 19
  • 16