Link to home
Start Free TrialLog in
Avatar of zorfael
zorfael

asked on

Calculate Pow() and Log() without using java.math

Hi there,
I needed to do some mathematics in an exercice but I am not allowed to use the java.math class, I need to calculate the power Pow() and logarithm log()  with a custom base, base 10 would work as well.
thank you so much!
Avatar of CSecurity
CSecurity
Flag of Iran, Islamic Republic of image

You can use this POW function:

Example 8
 
double pow(double a, double b)
{
    // true if base is greater than 1
    boolean gt1 = (Math.sqrt((a-1)*(a-1)) <= 1)? false:true;
         
    int oc = -1; // used to alternate math symbol (+,-)
    int iter = 20; // number of iterations
    double p, x, x2, sumX, sumY;
        
    // is exponent a whole number?
    if( (b-Math.floor(b)) == 0 )
    {
        // return base^exponent
        p = a;
        for( int i = 1; i < b; i++ )p *= a;
        return p;
    }
        
    x = (gt1)?
            (a /(a-1)): // base is greater than 1
            (a-1); // base is 1 or less
                
    sumX = (gt1)?
            (1/x): // base is greater than 1
            x; // base is 1 or less
        
    for( int i = 2; i < iter; i++ )
    {
        // find x^iteration
        p = x;
        for( int j = 1; j < i; j++)p *= x;
            
        double xTemp = (gt1)?
                (1/(i*p)): // base is greater than 1
                (p/i); // base is 1 or less
            
        sumX = (gt1)?
                (sumX+xTemp): // base is greater than 1
                (sumX+(xTemp*oc)); // base is 1 or less
                    
        oc *= -1; // change math symbol (+,-)
    }
        
    x2 = b * sumX;
        
    sumY = 1+x2; // our estimate
                
    for( int i = 2; i <= iter; i++ )
    {
        // find x2^iteration
        p = x2;
        for( int j = 1; j < i; j++)p *= x2;
            
        // multiply iterations (ex: 3 iterations = 3*2*1)
        int yTemp = 2;
        for( int j = i; j > 2; j-- )yTemp *= j;
            
        // add to estimate (ex: 3rd iteration => (x2^3)/(3*2*1) )
        sumY += p/yTemp;
    }
        
    return sumY; // return our estimate
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CSecurity
CSecurity
Flag of Iran, Islamic Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zorfael
zorfael

ASKER

Thank you for your effort however the "Math" class can never be mentioned and Pow() should work with numbers like 0.53232333 for example and I can't pass a number like that as "int". the Log() function should be base 10 or custom base. In fact I need Pow() to calculate Exponential, Sin, Cosine, if you guys had those functions as well that would be great but with just the Pow() my numbers will come out good because they do if I use math.pow().
any other solutions?
thank you so much
Avatar of zorfael

ASKER

actually I made it to work with double numbers using brunoguimaraes solution but I still need a logarithm function that would the same value as the function below but whithout using the Math class:

public double Log(double a, double base)
{
   double x;
   x = math.log10(a)/math.log(base)
}

any ideas?
return x;
}
Avatar of zorfael

ASKER

CSecurity Pow() solution works just like brunoguimaraes's solution when updated to use double instead of int.
My first example was in Double base itself:

double pow(double a, double b)
Avatar of zorfael

ASKER

sure, however you referenced "Math" on your code:
"Math.sqrt"
I can't use that but that's not the problem right now as I got that figured out,
I need to calculate the Logarithm in a custom base now, any ideas?
thank you!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zorfael

ASKER

thank you all!