Friday, August 14, 2009


As promised, I have returned to reveal the mystical secrets of the math universe. Yes, step right up, step right up!

So, now that we have the FLOOR(x) function, now we are going to build on that to create the CEIL(x) function.

FLOOR(x) = x- (arccos(cos(x*2pi))/2pi)*IS_POS - (1-arccos(cos(x*2pi))/2pi)*(NOT(IS_POS))

IS_POS was our switch function that equaled 1 when sin(x*2pi) was positive or 0. Using the fact that 0^0 = 1 and 0^(any other positive number) = 0, it went like this:
IS_POS = 0^abs(sin(x*2pi)-abs(sin(x*2pi)))

And the NOT(x) function there could be implemented a few ways. In the floor function post, I again used the properies of 0 raised to flip the IS_POS switch like this:

IS_POS = 1
0^abs(IS_POS) = 0^1 = 0

IS_POS = 0
0^abs(IS_POS) = 0^0 = 1

Perdy neat!

But this time, and for no real particular reason except to be explorative, we'll redefine the NOT(x) function.

Using the constraint of IS_POS (it can only be 0 or 1), we can make the function:

IS_POS = 1
NOT(IS_POS) = 1 - IS_POS1 = 1-1 = 0

IS_POS = 0
NOT(IS_POS) = 1 - IS_POS1 = 1-0 = 1

This is an old computer programming trick.

Anyway, back from the tangent to the task at hand; Developing a working CEIL(x) function! I just wanted to get everybody back on the same page.

So looking at FLOOR(x) again:
FLOOR(x) = x- (arccos(cos(x*2pi))/2pi)*IS_POS - (1-arccos(cos(x*2pi))/2pi)*(NOT(IS_POS))

we have these switches on two very similar looking chunks of the equations-


Well, the first is "active" (multiplied by 1) when the IS_POS function is true and "deactivated" (multiplied by 0) when the IS_POS function is returning 0. Conversely the second is activated and deactivated oppositely, thanks to the NOT(x) function.

The reason for this is (explained by the following exaples of flooring 3.r were r is the decimal or fractional part of the number):
arccos(cos(3.00*2pi))/2pi = 0.00
arccos(cos(3.25*2pi))/2pi = 0.25
arccos(cos(3.50*2pi))/2pi = 0.50

so our neat little equation there returns just the fractional part of the number for us but, because the cos function is cyclical, after 3.5 up to 4.0, the result reverses:
arccos(cos(3.60*2pi))/2pi = 0.40, but +1-0.40 = 0.60
arccos(cos(3.75*2pi))/2pi = 0.25, but +1-0.25 = 0.75
arccos(cos(3.90*2pi))/2p) = 0.10, but +1-0.10 = 0.90

So, you see, for 3.0 to 3.5, we want to just straight subtract off the result, but once we are past 3.5 but less than 4 (when the IS_POS is false), we need to subtract off the difference between 1 to make the floor function work.

Ok, so we are all caught up on how it works, so let's use these blocks to make the CEIL function!

CEIL(x) = x+what?

Well, let's see what CEIL(x) should return (using 3.r again):
CEIL(3.00) = 3.00
CEIL(3.25) = 4.00
CEIL(3.50) = 4.00
CEIL(3.75) = 4.00
CEIL(4.00) = 4.00

So now, we need to add, but let's lookj at what we need to add:
CEIL(3.00) = 3.00, add 0.00
CEIL(3.25) = 4.00, add 0.75
CEIL(3.50) = 4.00, add 0.50
CEIL(3.75) = 4.00, add 0.25
CEIL(4.00) = 4.00, add 0.00

Well, it looks like we are adding 1-r (remember that r was the fractional part) so:
CEIL(x) = x+(1-(arccos(cos(x*2pi))/2pi))

Let's test it and make sure:
CEIL(3.25) = 3.25+(1-(arccos(cos(3.25*2pi))/2pi)) = 3.25+0.75 = 4.0, Good
CEIL(3.50) = 3.50+(1-(arccos(cos(3.50*2pi))/2pi)) = 3.50+0.50 = 4.0, Good
CEIL(3.75) = 3.75+(1-(arccos(cos(3.75*2pi))/2pi)) = 3.75+0.75 = 4.5, oops!
CEIL(4.00) = 4.00+(1-(arccos(cos(5.00*2pi))/2pi)) = 4.00+1.00 = 5.0, oops!

So what went wrong? Well, 2 things:
1) Integers (like 3.0 and 4.0) should stay as they are, not have 1 added to them
2) In the CEIL(3.75) example, the old cos function being cyclical thing got us again.

so, for 3.5 to 4.0 and on the integers(3.00 and 4.00 for example) we want to add just the fractional part, not the 1-fractional. This is very much the opposite of what we were seing before. Ofcourse that makes sence in that the FLOOR(x) and the CEIL(x) are opposites.
So let's go crazy and create an IS_NEG function that is equal to 1 when sin(x*2pi) is negative or 0!

IS_NEG = 0^abs(-sin(x*2pi)-abs(sin(x*2pi)))

What's the difference from IS_POS? I multiplied the first sin * -1. Now, if sin(x*2pi) is positive, then it becomes a negative and then subtracting off the abs value will not be 0, and because 0 raised to anything but 0 equals 0, IS_NEG will be False ( or 0).

This means that IS_NEG will equal 1 when sin(x*2pi) equals 0 or is negative. Perfect.

So let's work the rest of the peices out:

CEIL(x) = x+ (arccos(cos(x*2pi))/2pi)*IS_NEG + (1-arccos(cos(x*2pi))/2pi)*(NOT(IS_NEG))

And there we go. A working CEIL(x) function.

No comments:

Post a Comment