java.lang.Math is just a port of what the C math library does.

For C, I think it comes down to the fact that CPU have special instructions to do Math.pow for floating point numbers (but not for integers).

Of course, the language could still add an int implementation. BigInteger has one, in fact. It makes sense there, too, because pow tends to result in rather big numbers.

ceil and floor by definition return integers, so how come they don't return ints

Floating point numbers can represent integers outside of the range of int. So if you take a double argument that is too big to fit into an int, there is no good way for floor to deal with it.

Answer from Thilo on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_math_pow.asp
Java Math pow() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate · ❮ Math Methods · Raise different numbers to different powers: System.out.println(Math.pow(2, 8)); System.out.println(Math.pow(3, 4)); System.out.println(Math.pow(9, 0.5)); System.out.println(Math.pow(8, -1)); System.out.println(Math.pow(10, -2)); Try it Yourself » ·
🌐
Baeldung
baeldung.com › home › java › java numbers › using math.pow in java
Using Math.pow in Java | Baeldung
January 8, 2024 - Here we’re not casting the result to an int as we are interested in a double value. Since we have a double value, we can easily configure and use a DecimalFormat to round the value to two decimal places, resulting in 74.09: DecimalFormat df = new DecimalFormat(".00"); double dblResult = Math.pow(4.2, 3); In this quick article, we have seen how to use the Java’s Math.pow() method to calculate the power of any given base.
Top answer
1 of 5
4

java.lang.Math is just a port of what the C math library does.

For C, I think it comes down to the fact that CPU have special instructions to do Math.pow for floating point numbers (but not for integers).

Of course, the language could still add an int implementation. BigInteger has one, in fact. It makes sense there, too, because pow tends to result in rather big numbers.

ceil and floor by definition return integers, so how come they don't return ints

Floating point numbers can represent integers outside of the range of int. So if you take a double argument that is too big to fit into an int, there is no good way for floor to deal with it.

2 of 5
2

From a mathematical perspective, you're going to overflow your integer if it's larger than 231-1, and overflow your long if it's larger than 264-1. It doesn't take much to overflow it, either.

Doubles are nice in that they can represent numbers from ~10-308 to ~10308 with 53 bits of precision. There may be some fringe conversion issues (such as the next full integer in a double may not exactly be representable), but by and large you're going to get a much larger range of numbers than you would if you strictly dealt with integers or longs.

On a similar topic, ceil and floor by definition return integers, so how come they don't return ints?

For the same reason outlined above - overflow. If I have an integral value that's larger than what I can represent in a long, I'd have to use something that could represent it. A similar thing occurs when I have an integral value that's smaller than what I can represent in a long.

🌐
GeeksforGeeks
geeksforgeeks.org › java › math-pow-method-in-java-with-example
Math pow() Method in Java with Example - GeeksforGeeks
March 28, 2025 - Example 1: This example demonstrates how to use the Math.pow() method in Java to calculate the power of a number (base raised to the exponent).
🌐
CodeGym
codegym.cc › java blog › java math › math pow() method in java
Math.pow() Method in Java
December 5, 2024 - This is because if you raise any number to the power of 1, the result is the same as the base. If the base is negative/positive zero and the exponent parameter is a negative number, then the result is Infinity. (Negative zeros can occur due to the rounding of numbers between zero and the smallest representable negative non-zero number). If the exponent parameter is NaN, the output will also be NaN. Let’s consider one instance where this 3rd situation can happen. import java.lang.Math; public class MyClass{ public static void main(String []args){ double base = 5; double exponent = Double.NaN; double answer = Math.pow(base, exponent); System.out.println(answer); } } This will output NaN.
🌐
Codecademy
codecademy.com › docs › java › math methods › .pow()
Java | Math Methods | .pow() | Codecademy
June 23, 2025 - Math.pow() is the standard way to perform exponentiation in Java, providing accurate results for both integer and floating-point operations.
Find elsewhere
🌐
Octoperf
blog.octoperf.com › java-mathpow-through-code-examples
Java Math.pow Through Code Examples - OctoPerf
March 16, 2018 - package com.octoperf; import org.junit.Test; import static java.lang.Double.NEGATIVE_INFINITY; import static java.lang.Double.NaN; import static java.lang.Double.POSITIVE_INFINITY; import static org.junit.Assert.assertEquals; public class MathPowTest { private static final double DELTA = 0.001d; @Test public void simpleExample() { assertEquals(1024d, Math.pow(2d, 10d), DELTA); } } The 10th power of 2 is 1024. The result is a double. Beware of casting the result to: an integer: it could overflow, which means the double value does not fit into the int value.
🌐
LabEx
labex.io › tutorials › java-how-to-cast-the-result-of-the-pow-method-to-an-integer-type-in-java-413943
How to cast the result of the pow() method to an integer type in Java | LabEx
To cast the result of the pow() method to an int type, you can use the following syntax: ... int base = 2; int exponent = 5; int result = (int) Math.pow(base, exponent); System.out.println(result); // Output: 32
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Math.html
Math (Java Platform SE 8 )
October 20, 2025 - if the second argument is a finite ... first argument to the power of the second argument · if the second argument is finite and not an integer, then the result is NaN. If both arguments are integers, then the result is exactly equal to the mathematical result of raising the first ...
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › pow
Java Math pow() - Calculate Power
December 3, 2024 - int intBase = 5; int intExponent = 2; double result = Math.pow(intBase, intExponent); System.out.println("5 raised to the power of 2 is: " + result); Explain Code · In this example, even though Math.pow() expects double arguments, Java's automatic type conversion allows integers to be used directly.
🌐
Educative
educative.io › answers › how-to-use-the-mathpow-method-in-java
How to use the Math.pow() method in Java
The Math.pow() is an built-in method in Java Math class and is used to calculate the power of a given number.
🌐
Programiz
programiz.com › java-programming › library › math › pow
Java Math pow()
int a = 2; int b = 5; Math.pow(a, b); // returns 32.0 · Also Read: Java Math.cbrt() Java Math.sqrt() Previous Tutorial: Java Math.cbrt() Next Tutorial: Java Math.subtractExact() Share on: Did you find this article helpful? Your builder path starts here. Builders don't just know how to code, they create solutions that matter.
🌐
CodeAhoy
codeahoy.com › java › Math-Pow-method-JI_11
Java Math.pow() Method with Examples | CodeAhoy
October 26, 2016 - int result = (int)Math.pow(3, 2) // result = 9 · Here’s a complete example that also shows the special case when the second argument is NaN. import static java.lang.Double.NaN; public class Main { public static void main(String[] args) { System.out.println("10^4 = " + (long) Math.pow(10, 4)); // 10000 System.out.println("2^4 = " + (long) Math.pow(2, 4)); // 16 System.out.println("2^1 = " + (long) Math.pow(2, 1)); // 2 System.out.println("2^0 = " + (long) Math.pow(2, 0)); // 1 // If the second argument is NaN, then the result is NaN.
🌐
GeeksforGeeks
geeksforgeeks.org › java › biginteger-pow-method-in-java
BigInteger pow() Method in Java - GeeksforGeeks
July 11, 2025 - The java.math.BigInteger.pow(int exponent) method is used to calculate a BigInteger raise to the power of some other number passed as exponent whose value is equal to (this)exponent.
🌐
Mkyong
mkyong.com › home › java › java – math.pow example
Java - Math.pow example - Mkyong.com
April 16, 2015 - package com.mkyong.test; import ... args) { //1. Math.pow returns double, need cast, display 256 int result = (int) Math.pow(2, 8); System.out.println("Math.pow(2, 8) : " + result); //2....
🌐
Spiceworks
community.spiceworks.com › programming & development
Math.pow() Method Not Operating Correctly? - Programming & Development - Spiceworks Community
July 15, 2015 - I’m new to Java. I am working with the Math.pow(); method, and I am not sure exactly what it is doing. For example, if I declare an integer called power that is equal to 2, then I use Math.pow(); to raise it by 2, to the power of 2. When I then print it, I still get 2. That definitely does ...
🌐
iO Flood
ioflood.com › blog › math-pow-java
Java's Math.pow() Function | Guide to Exponents in Java
February 29, 2024 - While Math.pow() is a powerful tool for exponentiation, it’s not the only way to perform this operation in Java. Let’s explore some alternative approaches, such as using loops or the BigInteger class. One straightforward method to calculate the power of a number is to use a loop. Here’s an example: int base = 2; int exponent = 3; int result = 1; for(int i = 0; i < exponent; i++) { result *= base; } System.out.println(result); // Output: // 8