Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua replacement for the % operator

Tags:

math

modulo

lua

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.

like image 234
Cyclone Avatar asked Mar 14 '12 03:03

Cyclone


People also ask

What is operator in Lua?

Lua supports the usual arithmetic operators. These operators are the binary + , - , * , / and ^ (exponentiation), and the unary - . If the operands are numbers, or strings that can be converted to numbers, according to the rules given in Section 4.2, all operations but exponentiation have the usual meaning.

Does Lua have modulo?

The Lua modulo operator is used to get the remainder of a division. The modulo operator is a built-in operator in the lua programming. Modulooperatoris an athematic operator and it is denoted by the % symbol. The modulo operation return the whole number remainder, instead of the division result.

What is modulus operator?

The modulus operator is added in the arithmetic operators in C, and it works between two available operands. It divides the given numerator by the denominator to find a result. In simpler words, it produces a remainder for the integer division. Thus, the remainder is also always an integer number only.

How do you say not equal to in Lua?

3.2 – Relational Operators The operator == tests for equality; the operator ~= is the negation of equality. We can apply both operators to any two values. If the values have different types, Lua considers them different values. Otherwise, Lua compares them according to their types.


6 Answers

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

like image 199
lhf Avatar answered Oct 06 '22 03:10

lhf


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

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

like image 28
ninesided Avatar answered Oct 06 '22 02:10

ninesided


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)

like image 45
Simon Forsberg Avatar answered Oct 06 '22 01:10

Simon Forsberg


function mod(a, b)
    return a - (math.floor(a/b)*b)
end
like image 34
Jim Gao Avatar answered Oct 06 '22 02:10

Jim Gao


for i = 1, 100 do
    if (math.mod(i,2) == 0) then
        print( i .. " is divisible.")
    end
end
like image 29
anonymous Avatar answered Oct 06 '22 01:10

anonymous


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

like image 29
vr3C Avatar answered Oct 06 '22 03:10

vr3C