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()

Post by intensity »

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

Re: math.floor()

Post by Exasperation »

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

Re: math.floor()

Post by TechnoCat »

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

Re: math.floor()

Post by fwoop »

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()

Post by intensity »

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

Re: math.floor()

Post by Exasperation »

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.
User avatar
VideroBoy
Party member
Posts: 102
Joined: Wed Mar 31, 2010 6:12 pm
Location: Canada

Re: math.floor()

Post by VideroBoy »

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.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: math.floor()

Post by Jasoco »

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)
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: math.floor()

Post by zac352 »

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. :P
Hello, I am not dead.
giniu
Party member
Posts: 221
Joined: Mon Nov 30, 2009 4:44 pm

Re: math.floor()

Post by giniu »

zac352 wrote:If you had any idea how base 2 is stored in the memory, you'd know why that happens. :P
it's just how floor and ceil are defined mathematically and not related to how it is stored - it's like saying that sky is blue because of rgb representation in computer, really. Just stick to definition from any (elementary I guess) school math book you might have avoided in past - or wikipedia page linked in third post.
Locked

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest