Tips for math functions

Hello! I need some help with the math.floor() function. I can’t understand stuff about it. I will really appreciate if some tips for this function is replied below. Thanks!

  • From AridFights1

EDIT: Here is the link to the math page.

2 Likes

math.floor(x) rounds x down, so math.floor(x + a) = x, where 1 > a >= 0 and x is an integer. For example:

  • math.floor(1.2) = 1
  • math.floor(1.99) = 1
2 Likes

Can you give me a small example for it like printing?

I edited my post with 2 examples.

1 Like

This means even if I try like

print(math.floor(2.99))

-- Output = 2?

Yes, all it does is round 2,99 down so you get 2 indeed.

1 Like

If you’re trying to round you can use math.floor as

math.floor (n + 0.5) -- round to nearest 0.5
2 Likes

math.floor rounds number down (hence the name floor)
to put it simply, it bassically just cuts whatever decimal you have

math.floor(5.5) -- 5
math.floor(10.1) -- 10
math.floor(100.99) -- 100

if your looking for the opposite and round numbers up, you can use math.ceil (ceiling)
to put it simply it cuts whatever decimal you have an adds 1

math.floor(5.5) -- 6
math.floor(10.1) -- 11
math.floor(100.99) -- 101
2 Likes

Similar functions are:

  • math.ceil(x): rounds x up (math.ceil(1.1) = 2
  • math.round(x): rounds x (math.round(1.5) = 2 and math.round(1.4) = 1)

Meaning if I put a decimal after the integer using the math.ceil() function, then the output result will be the next integer number?

if it is 2.1 then it takes away the 0.1 and pluses 1 essentially

1 Like

Thank you all! Got some really good tips from here. Appreciated all of your help!

So you don’t get confused math.floor() crunches a number to the next integer (whole number) less than or equal to itself. This way you don’t find negative numbers confusing because math.floor(-0.2) will be -1.

If you think of numbers in a number line:


-5
-4
-3
-2
-1
0
1
2
3
4
5

math.floor() will push upwards if the number is in between (i.e. math.floor(1.2)) is between 1 and 2 so it will crunch up to 1.

1 Like

To be sure with all the examples I got in this post please check it if it’s wrong or correct;

print(math.floor(2.5))
-- Output = 2
print(math.ceil(2.5))
-- Output = 3
print(math.round(2.4))
-- Output = 2
print(math.round(2.5))
-- Output = 3

So that means that if I try print(math.floor(-5.2)) result will be -6?

Yes it crunched upwards towards negative (-) infinity.

1 Like

Thanks! It helped me a lot! Appreciated all of your help!

Hey all! Been a little while and also sorry for bumping here. But can y’all help me with me with some more functions of math? For example math.clamp What does it do?

math.clamp is used to keep a number within your given range. It expects 3 arguments, the number to clamp, the minimum the number can be, and the maximum it can be. If the number is below the minimum or above the maximum, it’ll set the number to the minimum or maximum respectively.

Example

print(math.clamp(5, 1, 10)) --Prints 5
print(math.clamp(1, 2, 10)) --Prints 2
print(math.clamp(15, 1, 10)) --Prints 10

Really old topic, but need help again.