Wednesday, August 12, 2009

FLOOR(X) - And so the saga continues...

So, the ultimate answer to the ultimate question of life, the universe and everything is...

Oops, wrong story.

But in the last episode, I was looking for a signal that would tell me if I was greater that half way around the circle, so I could signal the arccos function to keep increasing, and that answer is sin.

Sin, being the y axis vector, is positive while traversing the circle's circumference counterclockwise for half the journey, and then becomes negative. That is exactly the indicator I need, but how can i make it work for me?

Well, there is an odd, and little known, fact about exponents, that goes something like this:

x^0=1 (any number raised to the 0th power is 1)
0^x = 0 (0 raised to any power is zero)

putting these two equations at conflict. To solve this, it was determined that exponents actually take the form:

that says, recursively, start with 1, and then multiply x to it, y times. Here's an example:

5^3 = 1 * 5 *5 * 5 = 125
5^2 = 1 * 5 * 5 = 25
5^1 = 1 * 5 = 5
5^0 = 1

or for 0:

0^3 = 1 * 0 * 0 * 0 = 0
0^2 = 1 * 0 * 0 = 0
0^1 = 1 * 0 = 0
0^0 = 1

Very interesting, but more importantly, this acts like a built in mathematical switch! In order to use this switch, we write an equation for the power applied to 0, and we know that the total result will be 0, until the equation is equal to 0, thus making 0^0 = 1.

Let me use an example to clarify:

Say I revisited my original goal to find an equation that would indicate if a number is an integer or not:

Well, thinking about the trig functions and the unit circle,

sin(n*pi) = 0

n representing only integers. Non integers will return a number other than 0. Again, an example to clarify:

sin( 0.0 * pi) = 0
sin( 0.5 * pi) = 1
sin( 1.0 * pi) = 0
sin( 1.5 * pi) = -1
sin( 2.0 * pi) = 0

this seems to meet our conditions for this switch. We wanted 0^0 only when there is an integer, so a true integer detection equation would be:


well, almost. You can't have a negative power (remember, roots are fractional powers, not negative) with 0^x or you would get div-by-zero, so an easy solution to this is the absolute value function, which basically removes the negative sign from all negative numbers. So, we really need:



So you get the jist of how this switch thingy-ma-bobber works, right? So back to the FLOOR(X) function. I currently have:

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

which works as long as the fractional part of x is 1/2 or less. So, using my switch and knowing the sin funxtion becomes negative when the fractional part of x is greater that 1/2, i can use a switch like:


to tell me if sin(x*2pi) >= 0.

This works because:

arccos(cos(3.2*2pi))/2pi = 0.2 which requires us to subtract this result from the 3.2 to floor it.
arccos(cos(3.5*2pi))/2pi = 0.5 which requires us to subtract this result from the 3.5 to floor it.


arccos(cos(3.6*2pi))/2pi = 0.4
arccos(cos(3.7*2pi))/2pi = 0.3
arccos(cos(3.8*2pi))/2pi = 0.2
arccos(cos(3.9*2pi))/2pi = 0.1

we now have to subtract 1-this result from the number we entered to floor it:

1-arccos(cos(3.6*2pi))/2pi = 0.6 and 3.6 - 0.6 = 3
1-arccos(cos(3.7*2pi))/2pi = 0.7 and 3.7 - 0.7 = 3
1-arccos(cos(3.8*2pi))/2pi = 0.8 and 3.8 - 0.8 = 3
1-arccos(cos(3.9*2pi))/2pi = 0.9 and 3.9 - 0.9 = 3

This new development is why we need the mathematical switch; without it, we have a piecewise function that isn't subject to all of the normal algebreaic motions. We can't reduce in a more complex equation, or apply commutative properties, etc. With the switch in place, however, it's game on.

So now we need the switch implimented:

x-arccos(cos(x*2pi))/2pi or x+1-arccos(cos(x*2pi))/2pi

to make it readable, I'm going to substitute the label IS_POS for the switch:

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

so we getsomething roughly like:

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

Ok, ok. I slipped one in there on ya. The not function basically says if IS_POS is 0, make it a 1, otherwise, make it a 0. Hmm, kinda like a switch...

NOT(a) = 0^abs(a)

so you see, the equation can be written:

x- (arccos(cos(x*2pi))/2pi)*IS_POS - (1-arccos(cos(x*2pi))/2pi)*0^abs(IS_POS)

but NOT is more human readable.

Enough for now, in the next post, I will make the CEIL(x) function work from these same blocks and then later I will write up the various MOD(x,y) functions.

No comments:

Post a Comment