ID:135330
 
I'd really like to see these functions in place or in addition to the round() function. Of course, if there is a way to simulate these with round() please feel free to correct me! :)
Of course, if there is a way to simulate these with round() please feel free to correct me! :)

Well right now round() does return the floor of a number despite what the reference says which has caused me problems on several occations :P.
proc/floor(number as num)
return round(number)

proc/ceil(number as num)
return floor(number)==number ? number : floor(number)+1


You can do ceil with less code, but that should work.
[edit]Fixed buggy not-working ceil(). Hope this one works. :)[/edit]
In response to Theodis (#1)
Theodis wrote:
Of course, if there is a way to simulate these with round() please feel free to correct me! :)

Well right now round() does return the floor of a number despite what the reference says which has caused me problems on several occations :P.

The reference is quite clear, and it works exactly as the reference states. round(n) is the integer portion of the number (floor). round(n,1) rounds the number to the nearest whole number.

round(5.6) == 5
round(5.6,1) == 6

Easy! :)
In response to Jon88 (#2)
Your ceil doesn't quite work. :)

proc/ceil(n)
var/c = round(n)
if (c < n) c++
return c
In response to Mike H (#3)
The reference is quite clear, and it works exactly as the reference states. round(n) is the integer portion of the number (floor).

The integer portion is not the floor. For instance round(-0.5) returns the floor which is -1 but the integer portion is 0. This is what has caused me problems when I really wanted the integer portion :).
In response to Theodis (#5)
Theodis wrote:
The reference is quite clear, and it works exactly as the reference states. round(n) is the integer portion of the number (floor).

The integer portion is not the floor. For instance round(-0.5) returns the floor which is -1 but the integer portion is 0. This is what has caused me problems when I really wanted the integer portion :).

Ah, very good. I vaguely remember you talking about this before but in my simple tests this afternoon, negative numbers didn't come up. Now I remember that this was the issue.

The internal code for round() calls floor(), so it's definitely returning the floor of the number in that case. Since it's quite possible that people are relying on the current behavior, it's easier to change the reference to be more accurate. I presume you've written your own truncate() by now. :)
In response to Mike H (#6)
Bah, I've been complaining about this ever since I wrote SET.
I believe I have floor() and ceil() procs in there if anyone needs them.