Tuesday, April 17, 2012

Non-algorithmic floor and ceiling functions

It's always kind of annoyed me when you need just the first digit of a decimal or you want to round up to the next whole number that you always seem to have to use floor or ceiling functions that you can't use ordinary techniques with because they are sort of outside of usual calculus. So anyway I came up with these conversions:

Floor(t) = t-u(t) where u(t) =
You see, if your t = something like 4.673, at that point this function equals .673, so you just subtract that and get your floor value.

Then there's the Ceiling function or round up,
Ceil(t) = t+U(t) where U(t) =
They are kind of messy but at least it gives you the power of floor and ceiling without stepping outside of calculus.
* One caveat is that the function is undefined at the exact whole numbers. I guess it depends on how you define floor whether the floor of 4.0 would be 3.0 or 4.0 but with these equations putting in whole numbers is equivalent to a divide by 0.
*If you look on wikipedia you can see the equations that led to these equations, they are the result of an infinite sum of sin waves (you have to scroll a ways down under "Series Expansion),
http://en.wikipedia.org/wiki/Floor_and_ceiling_functions
but they don't mention that that infinite sum has a closed form and the problem they mention of the whole numbers giving a value halfway in between doesn't come up, these are instead undefined there.