math.floor()
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.
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
examples:
floor(3.14159) returns 3
floor(4.99999998) returns 4
floor(5) returns 5
- Exasperation
- Prole
- Posts: 11
- Joined: Sat Oct 02, 2010 7:11 pm
Re: math.floor()
That's only true for non-negative numbers. floor(-0.5) is -1, not 0.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
Re: math.floor()
This needs to be emphasized. I ran into many bugs when I used floor without realizing this.Exasperation wrote:That's only true for non-negative numbers. floor(-0.5) is -1, not 0.
- Jasoco
- Inner party member
- Posts: 3727
- 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)
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()
If you had any idea how base 2 is stored in the memory, you'd know why that happens.Exasperation wrote:That's only true for non-negative numbers. floor(-0.5) is -1, not 0.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
Hello, I am not dead.
Re: math.floor()
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.zac352 wrote:If you had any idea how base 2 is stored in the memory, you'd know why that happens.
Who is online
Users browsing this forum: No registered users and 10 guests