Page 1 of 3

math.floor()

Posted: Mon Oct 04, 2010 12:47 am
by intensity
What does the math.floor() function do?

Re: math.floor()

Posted: Mon Oct 04, 2010 1:05 am
by Exasperation
The floor of a number is the greatest integer less than or equal to that number.

Re: math.floor()

Posted: Mon Oct 04, 2010 1:12 am
by TechnoCat

Re: math.floor()

Posted: Mon Oct 04, 2010 1:16 am
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

Re: math.floor()

Posted: Mon Oct 04, 2010 1:16 am
by intensity
Thanks.

Re: math.floor()

Posted: Mon Oct 04, 2010 1:18 am
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.

Re: math.floor()

Posted: Mon Oct 04, 2010 9:20 pm
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.

Re: math.floor()

Posted: Mon Oct 04, 2010 10:37 pm
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)

Re: math.floor()

Posted: Wed Oct 06, 2010 9:58 am
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

Re: math.floor()

Posted: Wed Oct 06, 2010 10:43 am
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.