ID:195144
 
//Title: Supplemental Rounding Utilities Snippet
//Credit to: Jtgibson (slightly modified by Hiead)
//Contributed by: Jtgibson

/*
These are a couple of supplemental rounding aliases designed to add to the
already extremely useful round() proc. You can use these in situations where
you want to fudge the number upwards or downwards as desired.

Remember that these are what would be called "inline" functions. They are more
efficient than standard functions because the code in them is copied and pasted
in the line whereever they are used, rather than "jumped to", run separately,
and then "jumped back from". This prevents the CPU from having to spend effort
hopping around inside your code -- though, in BYOND's case, this is unlikely to
matter much.

Note that these defines also work in most other full programming languages!
*/



#define ceiling(X) ( (round(X) != (X)) ? (round(X) + 1) : (X) )
#define floor(X) round(X)
#define round_near(X) round((X),1)


///*
//Testing code/sample implementation:

mob/verb/test_rounding()
var/num = rand(1,1000) + (rand(1,1000)/1000)
if(prob(50)) num *= -1 //allow negative numbers

usr << "<b>Number: [num]</b>"
usr << "--> Rounded up: [ceiling(num)]"
usr << "--> Rounded down: [floor(num)]"
usr << "--> Rounded to the nearest: [round_near(num)]"
//*/
Just a small question... :)

Jtgibson wrote:
#define ceiling(X) ( (round(X) == 0 && (X) != 0) ? 1 : ((X)/round(X) != 1 ? \
> round((X)+1) : round(X)) )


Why so complicated? :,( It could be done with a simple ternary calculation (as opposed to a nested one).

#define ceiling(X) ( (round(X) != (X)) ? (round(X) + 1) : (X) )


My guess is that you really wanted to use division to check for equality (how silly :P), which meant that you had to test for 0 separately. Still, DM does have a nifty inequality test that could be applied so that one doesn't have to divide (making sure to watch for divide-by-0 errors --- overly complicating a simple procedure).

Don't mean to seem like a prick (what with already changing a snippet before merely because it used an obsolete proc), but I do think it would be better to try and simplify things for those people that come upon it (and I'm not going to go and market a whole new "Modified" rounding utility just for the sake of simplifying this one), right?

Hiead
In response to Hiead
I seem to recall I did it that way to handle negative numbers or some such. I'm not sure what was going on back then. Like most of my other snippets, this was written at least four years ago. ;-)

After some testing, it seems like yours works just fine. I went ahead and made the change.

*shrugs* I'm more of a conceptual programmer. I think in terms of what I can accomplish, not in terms of how to accomplish it. ;-)


Before you get any wrong ideas, I do appreciate having my stuff optimised! I haven't had a lick of training for O(n) and stuff and such*, so for the most part I just think in terms of how many cycles a computer takes to do what I want it to and if it's too high for comfort, I'll try and think of another way. Of course, if it's merely "not bad" or if it's something I wouldn't be using once every cycle, I don't care about it, so it bugs me a bit if it's nothing important (a string reverse procedure? come on ;-)).

You'll note I showcased Lummox JR's improvements over my original cone proc, too. =)


* I felt pretty gypped when I took the AP Computer Science exam and discovered half of the test was about that -- my CS teacher didn't teach me anything about it. The course was an IB Computer Science course, which was probably part of the reason why...
In response to Jtgibson
Jtgibson wrote:
Of course, if it's merely "not bad" or if it's something I wouldn't be using once every cycle, I don't care about it, so it bugs me a bit if it's nothing important (a string reverse procedure? come on ;-)).

And then some day, far in the future, somebody else at your place of employment will go looking for a function to accomplish something. Even if it's a one-off procedure you did even though in reality that code will probably never be called.

http://blogs.msdn.com/larryosterman/archive/2005/10/14/ 481137.aspx
In response to Jon88
If I was working at a place that was using a string reverse procedure in an inner loop, I'd quit. =)