Announcement

Collapse
No announcement yet.

How do fractions fit in when doing x^y?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do fractions fit in when doing x^y?

    How do the fractions fit into the mathematics when raising a number to a power, as in x^y, and the fractional part would be in y? I've been having fun playing with the source code that Mr. Eddy Van Esch posted for doing fractions with the HIME Huge Integers, including making my modifications of his code so they use signed integers. But I am completely hung up on implementing an x^y function for use with the code. Making it a wrapper for the native hi_Pow function is out of the question, as the numbers get way, way too big (i.e., it stalls everything to a grinding halt). I did some research by Googling, and I have seem to come up with a recursive algorithm for obtaining a result from an x^y, and it seems fast enough, but, it simply doesn't return correct results when the exponents have fractional parts. I am comparing these results with the results of using the PB e1##^e2## type code, and they don't match at all.

    Any help *gratefully* received.

  • #2
    Ok, don't need replies right now, got more research to do. I just found out that x^1/n is the same thing as finding the nth root. OK, now I'll try to find an implementation for that.

    Comment


    • #3
      Originally posted by Clay Clear View Post
      Ok, don't need replies right now,
      Will you forgive me ..? :laugh:
      In a nutshell: x^y for x and y being real (fractional) numbers is usually done by using e^A where A = y * log(x) and e = 2.718282....
      e^A in its turn is calculated by using Taylor series evaluation, which is an iterative algorithm.

      I have a new library in development which will do arbitrary precision real (floating point) number calculations, but in a different way than I used in the (not-so-quick and dirty) way in the demo program in the source code forum ..

      Kind regards
      Eddy

      Comment


      • #4
        Thanks, Eddy. Yer fergiven.

        Yup, I've been aware of your new library since you plugged it in the Third Party Forum, and it's my intentions to purchase a copy when it's ready. 'Course, plans change, but at this time that's what they are.

        Oh, could you make the e-mail adx in my signature the one you have in my customer records? I had forgotten that you had problems with my e-mail. Sorry about that.

        Thanks for the explanation into the math. I guess I will just leave my HiFP_Pow function as is. Your posting in the Source Code Forum gave me the impetus to write my own calculator program, to replace the Windows Cal.exe program, which is why I was keen to get the x^y stuff working. Oh, well, my workaround does the job for now (combination of PB's x^y and my huge integer version - the function makes the decision which one to use based on whether y has a fractional part. If it does not, it use the hime version. It also falls back to the hime version if the e1## variable overflows when doing the exponentiation. But then it has to strip the fractional part from y. )

        Comment


        • #5
          Originally posted by Clay Clear View Post
          I just found out that x^1/n is the same thing as finding the nth root. OK, now I'll try to find an implementation for that.
          You can do x^y when x and y are fractional numbers using HIMEs hi_Pow and hi_Root_N like this:

          example.
          Say function Nroot(N, x) returns the Nth root of x (that would be HIME function hi_Root_N).

          Calculate 5^3.26 (let's keep x an integer number for ease of explanation)
          = 5^3 * 5^0.26

          --> 5^0.26 = 5^(26/100) = Nroot(100, (5^26) ) or better (read 'faster'): (Nroot(100, 5))^26

          Now, Nth root when N is large will not be fast, so to speed up, consider that:
          Nroot(100 , a) = Nroot(10, Nroot(10, a))
          Nroot(1000, a) = Nroot(10, Nroot(10, Nroot(10, a)))
          and so on.

          To summarise:
          5^3.26 = 5^3 * (Nroot(10, Nroot(10, 5)))^26

          Now remember that if you do this by using the trick I showed in this Arbitrary precision fixed point demo , numbers will get big very fast.

          For example, if you want to do: 5.123^3.26, you need to calculate (according the above and according the fixed point demo) 5123^3 .
          This already gives a 12 digit number. Of course, you can truncate the rightmost 6 digits of the result to go back to the 3 decimals accuracy used.
          --> 5123^3 = 134453795867
          --> 5.123^3 = 134.453795867 , after reducing accuracy to 3 decimals: 134.453

          To calculate the Nth root, it is the opposite, you have to add trailing zeroes before taking the root:
          To calculate the 10th root of 12452127.497 (assuming accuracy is still set at 3 digits):
          First make sure that there are 10 (N) * 3 (accuracy) = 30 decimal digits:
          12452127.497000000000000000000000000000
          Then remove the decimal point:
          12452127497000000000000000000000000000

          The 10th root of this number is 5122 .
          Correcting for 3 decimal accuracy gives 5.122 so:
          The 10th root of 12452127.497 is about 5.122 calculated using huge integer math.

          Kind regards
          Last edited by Eddy Van Esch; 23 Jan 2008, 07:32 AM.
          Eddy

          Comment


          • #6
            1. Z^(X/Y) = the Xth power of the Yth Root of Z

            2. The easy way to double-check your exponentiation is to use logarithms:
            Z^(X/Y) = ALOG ( X/Y * LOG (Z))
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Originally posted by Michael Mattias View Post
              1. Z^(X/Y) = the Xth power of the Yth Root of Z

              2. The easy way to double-check your exponentiation is to use logarithms:
              Z^(X/Y) = ALOG ( X/Y * LOG (Z))
              That's fine for math. But HIME doesn't know logarithms. (yet)
              Regards,
              Bob

              Comment


              • #8
                >That's fine for math. But HIME doesn't know logarithms. (yet)

                It doesn't need to. If you count decimal digits and use just the significant digits you can manage your own exponents.

                Just use the base 10 logarithm function LOG10 when you get the mantissa, and when you go the other way use EXP10, then add the correct number of zeroes on either size of the decimal point.
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  Am returning to this thread to say "thank you", Eddy. Thanks to your taking the time to explain the stuff, I now have working "hifp_pow" and "hifp_sqr" functions. I owe ya one.

                  Comment


                  • #10
                    You're welcome, Clay ...
                    Eddy

                    Comment

                    Working...
                    X