Close

Java - Math.pow() Examples

Java Java API 


Class:

java.lang.Math

java.lang.Objectjava.lang.Objectjava.lang.Mathjava.lang.MathLogicBig

Method:

public static double pow(double a,
                         double b)

Returns the value of the first argument raised to the power of the second argument.


Examples


package com.logicbig.example.math;

public class PowExample {

public static void main(String... args) {
findPow(3.0, 5.0);
findPow(9.0, 0.5);
findPow(532.5, 0.0);
findPow(-289.7, 1.0);
findPow(0.0, -1.0);
findPow(Double.MIN_VALUE, Double.NEGATIVE_INFINITY);
findPow(Double.NEGATIVE_INFINITY, 7.0);
}

private static void findPow(double x, double y) {
double result = Math.pow(x, y);
System.out.printf("Math.pow(%s,%s) = %s%n", x, y, result);
}
}

Output

Math.pow(3.0,5.0) = 243.0
Math.pow(9.0,0.5) = 3.0
Math.pow(532.5,0.0) = 1.0
Math.pow(-289.7,1.0) = -289.7
Math.pow(0.0,-1.0) = Infinity
Math.pow(4.9E-324,-Infinity) = Infinity
Math.pow(-Infinity,7.0) = -Infinity




package com.logicbig.example.math;

public class PowExample2 {

public static void main(String... args) {
roundUp(115.9711123, 3);
roundUp(91.1236234, 2);
roundUp(-139.5114223, 5);
roundUp(-0.1998234, 4);
}

private static void roundUp(double value, int decimalPlaces) {
int temp = (int) Math.pow(10, decimalPlaces);
double roundedValue = Math.ceil(value * temp) / temp;
System.out.printf("Rounding up %12s to %s decimal places = %s%n", value, decimalPlaces, roundedValue);
}
}

Output

Rounding up  115.9711123 to 3 decimal places = 115.972
Rounding up 91.1236234 to 2 decimal places = 91.13
Rounding up -139.5114223 to 5 decimal places = -139.51142
Rounding up -0.1998234 to 4 decimal places = -0.1998




See Also