Negative powers

This commit is contained in:
LinlyBoi
2022-01-29 17:32:40 +02:00
parent bbedf0d25b
commit 9aaca7c080
2 changed files with 19 additions and 12 deletions

View File

@@ -2,16 +2,29 @@ package MathRelated;
public class PowerOfSquares
{
public int IntegerPower(int base, int power)
public double IntegerPower(int base, int power)
{
if(power == 0)
return 1;
else if(power < 0) //unlucky this is not required scammed
{
power = Math.abs(power);
double result = 1;
int count = 1;
while(count <= power)
{
result /= base;
count++;
}
return result;
}
else
{
int count = 1;
int result = base;
double result = base;
while (count < power)
{
result = result * base;
@@ -19,16 +32,7 @@ public class PowerOfSquares
}
return result;
}
/* else //unlucky this is not required scammed
{
int count = -power;
while(count <= power)
{
base = base / base;
}
}
*/
}
public static void Square(int side , char c)

View File

@@ -10,9 +10,12 @@ public class PowerOfSquaresTest extends PowerOfSquares
@Test
public void integerPower()
{
System.out.println(IntegerPower(2,-1));
assertEquals(27,IntegerPower(3,3));
assertEquals(4,IntegerPower(2,2));
assertEquals(1,IntegerPower(2,0));
assertEquals(0.5 , IntegerPower(2,-1));
}
@Test