math.floor()
Posted: Mon Oct 04, 2010 12:47 am
What does the math.floor() function do?
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
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.
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
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.