I have made these two functions with the help of posts on math.stackexchange.com.

For ln I'm using information gathered from Calculate Logarithms by Hand and for calculating powers I'm using How to Calculate a Decimal Power of a Number

The functions I wrote seem to be working fine with smaller numbers like 32, which result in 9.00003048002563640458932185659665779695115234.

320 results in 3486902488.6640151119143428706046321880279236576908532 but when I compare this result with the built in language function I'm using the result is 3486784401 ; the difference between the integers is 118087.

It seems there's a limit to the methods I'm using but could I get this confirmed please and thanks? Or would there exist methods I could use to calculate more accurate* answers?

Psuedo code for my power function:

Comments used: https://math.stackexchange.com/a/21386/288031, https://math.stackexchange.com/a/180655/288031

function pow (a,b)
    stringify a and b;

    if b is 1 return a;
    else if b is 0 return 1;

    m => is the product of b and the custom natural log function of a;
    m is stringified;

    n => is the parsed integer value of m, then stringified;
    o => is the difference of m and n, which should result in just the decimal value;

    p => 1; // will be the stored product

        use i for our index (i starts at zero)
        loop while i is less than n

        p => is the product of p and euler's number

        i => add i and 1

    // the built-in euler's number I'm referring to is just the
    // built-in Math.exp(o) function which just avoids using 
    // built-in Math.pow( Math.E, o )
    return the product of p and built-in euler's number raised to the power of o

Pseudo code for my natural log function:

Using comment https://math.stackexchange.com/a/61283/288031

function ln (z)
    b => stores (z - 1) divided by (z + 1); // value doesn't change in the loop
    exp_counter => 1;
    current => 0;
    previous => -1;
    n => 0;

    c => 1;

    loop while current and previous do not equal // convergence
        previous => current;

        d => is the sum of 1 and (2 * n);
        a => is 1 / d;

        // power calculator without power function
        loop while exp_counter is less than d
            c => c * b;
            exp_counter => 1 + exp_counter;
        end loop

        current => current + (a * c);
        n => n + 1
    end loop

    return current * 2;

*Accuracy: I have a number of "items" (63) and I wanted to calculate the total combinations I could use these items; using binary bits to represent each 63 items I am using 263 for the answer. I used my custom functions to calculate the answer and compared with http://wolframalpha.com. My answer from the custom functions wont satisfy what I need in order to accurately represent the total combinations of the items. (answer I generated from 263 = 9232375753198674821.914432211635780578399921666955505030143435683)

  • $\begingroup$ Define "accurate". Also, define what method you're using specifically. There are several different ones on the pages you linked to. $\endgroup$ – CaptainObvious Dec 27 '15 at 6:31
  • $\begingroup$ Isn't there a different place for math that involves coding? +1 for trying :) $\endgroup$ – Simply Beautiful Art Dec 27 '15 at 16:34
  • $\begingroup$ :D I almost considered CodeReview -- literally copied and pasted what I wrote there and switched to Math $\endgroup$ – ŽaMan Dec 27 '15 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.