## Making a copy of the Math.pow() method

math.pow java
math.pow() c#
math.pow in python
math.pow c++
java program to find power of a number using for loop
how to implement power function in java
what does math.pow do in java
double to int in java

I'm making a method that's supposed to do the same as Math.pow(), but I just can't figure out how to make it possible to use double values for y and get a double value result with decimals.... Any ideas (preferably using a for-loop)? In the method below I used "x" as the base and "y" as the exponent.

```public static double power(double x, double y) {
double result = 1;
if (y <= 0)
return 0;
for (int count = 0; count < (int)y; count++)
result *= x;
return result;
}
```

You could use Math.log and Math.exp to acheive this.

```public static void main(String[] args) throws InterruptedException {

System.out.println(power(2,2.5));

}

public static double power(double x, double y) {

double val = y *  Math.log(x);

double result = Math.exp(val);

return result;
}
```

output is

```5.65685424949238
```

math.pow() method with example in Python, by design, but if you need an int then an explicit cast must be performed. Returns a specified number raised to the specified power. A double-precision floating-point number to be raised to a power. A double-precision floating-point number that specifies a power. The number x raised to the power y. The following example uses the Pow method to calculate the value that results from raising 2 to a power ranging from 0 to 32.

Creating a copy of math.pow that does not use other math functions requires a basic understanding of bitwise operations. I don't use java but here is the code in c#

```  public int ReturnPower(int firstNum, int secondNum)
{

int finalNum = 1;

while (secondNum != 0)
{

//Use the bitwise operator & to check both strings
if ((secondNum & 1) == 1)
{
finalNum = finalNum * firstNum;
}

//increment firstNum for the next iteration
firstNum = firstNum * firstNum;

//Bitwise operator righshift '>>' is used to reconfigure secondNum
secondNum = secondNum >> 1;
}

//return((int)Math.Pow(firstNum, secondNum));
return (finalNum);

}
```

**side note the last 2 lines would return the same output. The '(int)' is placed before Math.pow to typecast it to allow type 'int' to be passed instead of 'double'.

How do you do exponents without math POW in Java? In C#, Math.Pow () is a Math class method. This method is used to calculate a number raise to the power of some other number. public static double Pow (double base, double power) double base: It is a double-precision floating-point number which is to be raised to a power and type of this parameter is System.Double.

Here is a effective/verbose implement:

```public static double pow(double x, double y)
{
// Special cases first.
if (y == 0)
return 1;
if (y == 1)
return x;
if (y == -1)
return 1 / x;
if (x != x || y != y)
return Double.NaN;

// When x < 0, yisint tells if y is not an integer (0), even(1),
// or odd (2).
int yisint = 0;
if (x < 0 && floor(y) == y)
yisint = (y % 2 == 0) ? 2 : 1;
double ax = abs(x);
double ay = abs(y);

// More special cases, of y.
if (ay == Double.POSITIVE_INFINITY)
{
if (ax == 1)
return Double.NaN;
if (ax > 1)
return y > 0 ? y : 0;
return y < 0 ? -y : 0;
}
if (y == 2)
return x * x;
if (y == 0.5)
return sqrt(x);

// More special cases, of x.
if (x == 0 || ax == Double.POSITIVE_INFINITY || ax == 1)
{
if (y < 0)
ax = 1 / ax;
if (x < 0)
{
if (x == -1 && yisint == 0)
ax = Double.NaN;
else if (yisint == 1)
ax = -ax;
}
return ax;
}
if (x < 0 && yisint == 0)
return Double.NaN;

// Now we can start!
double t;
double t1;
double t2;
double u;
double v;
double w;
if (ay > TWO_31)
{
if (ay > TWO_64) // Automatic over/underflow.
return ((ax < 1) ? y < 0 : y > 0) ? Double.POSITIVE_INFINITY : 0;
// Over/underflow if x is not close to one.
if (ax < 0.9999995231628418)
return y < 0 ? Double.POSITIVE_INFINITY : 0;
if (ax >= 1.0000009536743164)
return y > 0 ? Double.POSITIVE_INFINITY : 0;
// Now |1-x| is <= 2**-20, sufficient to compute
// log(x) by x-x^2/2+x^3/3-x^4/4.
t = x - 1;
w = t * t * (0.5 - t * (1 / 3.0 - t * 0.25));
u = INV_LN2_H * t;
v = t * INV_LN2_L - w * INV_LN2;
t1 = (float) (u + v);
t2 = v - (t1 - u);
}
else
{
long bits = Double.doubleToLongBits(ax);
int exp = (int) (bits >> 52);
if (exp == 0) // Subnormal x.
{
ax *= TWO_54;
bits = Double.doubleToLongBits(ax);
exp = (int) (bits >> 52) - 54;
}
exp -= 1023; // Unbias exponent.
ax = Double.longBitsToDouble((bits & 0x000fffffffffffffL)
| 0x3ff0000000000000L);
boolean k;
if (ax < SQRT_1_5)  // |x|<sqrt(3/2).
k = false;
else if (ax < SQRT_3) // |x|<sqrt(3).
k = true;
else
{
k = false;
ax *= 0.5;
exp++;
}

// Compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5).
u = ax - (k ? 1.5 : 1);
v = 1 / (ax + (k ? 1.5 : 1));
double s = u * v;
double s_h = (float) s;
double t_h = (float) (ax + (k ? 1.5 : 1));
double t_l = ax - (t_h - (k ? 1.5 : 1));
double s_l = v * ((u - s_h * t_h) - s_h * t_l);
// Compute log(ax).
double s2 = s * s;
double r = s_l * (s_h + s) + s2 * s2
* (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
s2 = s_h * s_h;
t_h = (float) (3.0 + s2 + r);
t_l = r - (t_h - 3.0 - s2);
// u+v = s*(1+...).
u = s_h * t_h;
v = s_l * t_h + t_l * s;
// 2/(3log2)*(s+...).
double p_h = (float) (u + v);
double p_l = v - (p_h - u);
double z_h = CP_H * p_h;
double z_l = CP_L * p_h + p_l * CP + (k ? DP_L : 0);
// log2(ax) = (s+..)*2/(3*log2) = exp + dp_h + z_h + z_l.
t = exp;
t1 = (float) (z_h + z_l + (k ? DP_H : 0) + t);
t2 = z_l - (t1 - t - (k ? DP_H : 0) - z_h);
}

// Split up y into y1+y2 and compute (y1+y2)*(t1+t2).
boolean negative = x < 0 && yisint == 1;
double y1 = (float) y;
double p_l = (y - y1) * t1 + y * t2;
double p_h = y1 * t1;
double z = p_l + p_h;
if (z >= 1024) // Detect overflow.
{
if (z > 1024 || p_l + OVT > z - p_h)
return negative ? Double.NEGATIVE_INFINITY
: Double.POSITIVE_INFINITY;
}
else if (z <= -1075) // Detect underflow.
{
if (z < -1075 || p_l <= z - p_h)
return negative ? -0.0 : 0;
}

// Compute 2**(p_h+p_l).
int n = round((float) z);
p_h -= n;
t = (float) (p_l + p_h);
u = t * LN2_H;
v = (p_l - (t - p_h)) * LN2 + t * LN2_L;
z = u + v;
w = v - (z - u);
t = z * z;
t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
double r = (z * t1) / (t1 - 2) - (w + z * w);
z = scale(1 - (r - z), n);
return negative ? -z : z;
}
```

module, it is used to calculate the given power of a base, it accepts two numbers and returns the first number to the power of the second number in the float. The java.lang.Math.pow (double a, double b) returns the value of the first argument raised to the power of the second argument. If the second argument is positive or negative zero, then the result is 1.0. If the second argument is 1.0, then the result is the same as the first argument. If the second argument is NaN, then the result is NaN.

pow() is used to calculate a number raise to the power of some other number. This function accepts two parameters and returns the value of first parameter raised  Math pow() method in Java with Example The java.lang.Math .pow() is used to calculate a number raise to the power of some other number. This function accepts two parameters and returns the value of first parameter raised to the second parameter.

Math pow is a function which calculates the power of any base number in already experienced odd results when calling Math.pow method. public void simpleExample() { assertEquals(1024d, Math.pow(2d, 10d) Learn how to create, iterate, clone arrays and more. Request a demo Enterprise edition. The Math.pow() function returns the base to the exponent power, that is, base exponent, the base and the exponent are in decimal numeral system. Because pow() is a static method of Math , you always use it as Math.pow() , rather than as a method of a Math object you created ( Math has no constructor).

pow() , rather than as a method of a Math object you created ( Math has no constructor). If the base is negative and the exponent is not an integer,  Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.

##### Comments
• do you know Math.pow(2 , 2.5) is almost equal to sqrt(32) ?
• You are better off not doing it, or study math before doing it: netlib.org/fdlibm/e_pow.c
• Hehe, oops. Thanks for heads up either way.
• +1 for the link provided by @nhahtdh. This is how this is probably implemented (Java calls into the standard math libraries). Not at all trivial.
• Figuring out the math behind `pow` is not easy. Take a look at this answer.
• Depending on what kind of homework this is, this may a perfect answer or considered cheating.
• Now let's hope that the OP doesn't have to implement log and exp because (from what I can see in openJDK) they both seem quite complicated.
• Haha, it's not cheating.