Absolutely Mind Blasting

10/28/2010 06:59 InfamousNoone#1
Code:
        static double f(double x, int klimit)
        {
            double result = Double.NaN;
            double numerator, denominator;
            int k;

            if (Math.Abs(x) < 1)
            {
                result = 0;
                for (k = 0; k <= klimit; k++)
                {
                    numerator = Math.Pow(-1, k) * Math.Pow(x, 1 + (2 * k));
                    denominator = 1 + (2 * k);
                    result += (numerator / denominator);
                }
            }
            else if (Math.Abs(x) > 1)
            {
                result = 0;
                for (k = 0; k <= klimit; k++)
                {
                    numerator = Math.Pow(-1, k) * Math.Pow(x, (-1) - (2 * k));
                    denominator = 1 + (2 * k);
                    result += (numerator / denominator);
                }
                numerator = Math.PI * Math.Sqrt(x * x);
                denominator = x * 2;
                result = (numerator / denominator) - result;
            }
            else // x == 1
            {
                return 0.25 * Math.PI;
            }
            return result;
        }
Code:
        static double finv(double x, int klimit)
        {
            Func<int, int> factorial = (n) =>
            {
                int num = 1;
                for (int j = n; j > 0; j--)
                    num *= j;
                return num;
            };

            Func<double, int, double> g = (gx, gklimit) =>
            {
                double gres = 0;
                double gnum, gdenom;
                for (int gk = 0; gk <= gklimit; gk++)
                {
                    gnum = Math.Pow(-1, gk) * Math.Pow(gx, (2 * gk));
                    gdenom = factorial(2 * gk);
                    gres += (gnum / gdenom);
                }
                return gres;
            };

            Func<double, int, double> h = (hx, hklimit) =>
            {
                double hres = 0;
                double hnum, hdenom;
                for (int hk = 0; hk <= hklimit; hk++)
                {
                    hnum = Math.Pow(-1, hk) * Math.Pow(hx, (2 * hk) + 1);
                    hdenom = factorial((2 * hk) + 1);
                    hres += (hnum / hdenom);
                }
                return hres;
            };

            return (h(2 * x, klimit) / (g(2 * x, klimit) + 1));
        }
Code:
finv( f ( x , k ) , k )
Will give you the same result as the x parameter
Well, not the exact result, but as k (klimit) increases, accuracy increases.


How's this relevant to CO?
Well, if we build on the f() function a bit we can do this...
Code:
        static double f2(double y, double x, int klimit)
        {
            if (x > 0)
                return f(y / x, klimit);
            else if (y >= 0 && x < 0)
                return Math.PI + f(y / x, klimit);
            else if (y < 0 && x < 0)
                return (-Math.PI) + f(y / x, klimit);
            else if (y > 0 && x == 0)
                return Math.PI / 2;
            else if (y < 0 && x == 0)
                return (-Math.PI) / 2;
            return Double.NaN;
        }
Code:
        static int GetAngle(int X1, int Y1, int X2, int Y2)
        {
            return (int)Math.Round(f2(Y2 - Y1, X2 - X1, 5) * 180 / Math.PI);
        }
10/28/2010 07:06 Arcо#2
Easy geometry homework ftw.
10/28/2010 07:07 oten681#3
Umm wtf does this do lol?
10/28/2010 07:09 InfamousNoone#4
Nothing -- well, that's not true but it's not worth explaining. I just found it highly fascinating.
10/28/2010 07:29 FuriousFang#5
Thank you for sharing that. I found it interesting because I didn't know much about the math class until now.
10/28/2010 10:00 _DreadNought_#6
Absoultely fantastic on how that works(Never done anything with maths(Well, not much)) May come in handy. #Bookmarked
10/28/2010 10:21 NukingFuts#7
nice and pretty fascinating
10/28/2010 13:29 _tao4229_#8
WHAT IS THIS I DONT EVEN ---
10/28/2010 22:13 InfamousNoone#9
I warned you that bricks would be shat.
10/29/2010 05:11 FuriousFang#10
Quote:
Originally Posted by copz1337 View Post
"Oh Absolutely" nerds these days.
Lol, srry that the math confuses you.
Noobs these days :rolleyes:

EDIT: xP i'm sorry, that was a bit harsh. Try and understand it. The result is interesting.
10/29/2010 13:25 _tao4229_#11
10 dollars says nobody that has posted in this thread understands it
10/29/2010 13:32 Korvacs#12
Quote:
Originally Posted by _tao4229_ View Post
10 dollars says nobody that has posted in this thread understands it
Agree.
10/29/2010 13:42 Basser#13
I don't understand it.
10/29/2010 13:53 DragonHeart#14
I got lost, sigh. Should get more into coding.
10/29/2010 14:01 Korvacs#15
This doesnt have very much to do with programming, you need to be good at maths to understand this.