## math.floor()

General discussion about LÖVE, Lua, game development, puns, and unicorns.
intensity
Prole
Posts: 8
Joined: Wed Sep 29, 2010 8:26 pm

### math.floor()

What does the math.floor() function do?
Exasperation
Prole
Posts: 11
Joined: Sat Oct 02, 2010 7:11 pm

### Re: math.floor()

The floor of a number is the greatest integer less than or equal to that number.
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

### Re: math.floor()

fwoop
Prole
Posts: 4
Joined: Fri Jun 25, 2010 3:59 pm

### Re: math.floor()

In layman terms, Floor cuts all the decimal rubbish off the end. Sometimes called truncation.

examples:
floor(3.14159) returns 3
floor(4.99999998) returns 4
floor(5) returns 5
intensity
Prole
Posts: 8
Joined: Wed Sep 29, 2010 8:26 pm

### Re: math.floor()

Thanks.
Exasperation
Prole
Posts: 11
Joined: Sat Oct 02, 2010 7:11 pm

### Re: math.floor()

fwoop wrote:In layman terms, Floor cuts all the decimal rubbish off the end. Sometimes called truncation.

examples:
floor(3.14159) returns 3
floor(4.99999998) returns 4
floor(5) returns 5
That's only true for non-negative numbers. floor(-0.5) is -1, not 0.
VideroBoy
Party member
Posts: 102
Joined: Wed Mar 31, 2010 6:12 pm

### Re: math.floor()

Exasperation wrote:That's only true for non-negative numbers. floor(-0.5) is -1, not 0.
This needs to be emphasized. I ran into many bugs when I used floor without realizing this.
Jasoco
Inner party member
Posts: 3655
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

### Re: math.floor()

Conversely math.ceil does the opposite. It rounds up..

2.3 = 3
6.9 = 7
5 = 5

To round to the nearest you'd pass the number and add .5 to math.floor...

math.floor(number + .5)
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Contact:

### Re: math.floor()

Exasperation wrote:
fwoop wrote:In layman terms, Floor cuts all the decimal rubbish off the end. Sometimes called truncation.

examples:
floor(3.14159) returns 3
floor(4.99999998) returns 4
floor(5) returns 5
That's only true for non-negative numbers. floor(-0.5) is -1, not 0.
If you had any idea how base 2 is stored in the memory, you'd know why that happens.