Math function, or Arithmetic Operator?

Hi,

So I’m wondering which I should use, there are Arithmetic Operators that do the exact same as a existing math function, so im wondering whether or not to use it?

-- Division (Remainder)

x  = 11
y  = 5

f1 = math.fmod(x, y) --> 1
f2 = (x % y)         --> 1

-- There is a math function known as 'fmod' which returns the remainder
-- There is an Arithmetic Operator known as 'Modulo' which returns the remainder
-- Exponent

x = 10
y = 2

p1 = math.pow(x, y) --> 100
p2 = (x^y)          --> 100

-- There is a math function known as 'pow' which returns x^y (x to the power of y)
-- There is a Arithmetic Operator known as 'Exponent' which returns x^y

I try to just use operators when I can, but in some cases it’s better to use math functions.

It’s all about personal preference, however the majority of people tend to use arithmetic operators due to the fact they are a lot easier to look at. Take for example, finding the remainder of x to the power y. One solution would look like such:

local answer = math.fmod(math.pow(x, y), z)

This is pretty bulky, however the other solution local answer = (x^y) % z is a lot easier to read.

There is no difference in performance as they both operate on the same code.

1 Like