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.
🌐
Octoperf
blog.octoperf.com › java-mathpow-through-code-examples
Java Math.pow Through Code Examples - OctoPerf
March 16, 2018 - package com.octoperf; import ... double. Beware of casting the result to: an integer: it could overflow, which means the double value does not fit into the int value....
Find elsewhere
🌐
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 odd integer, the result is equal to the negative of the result of raising the absolute value of the 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 argument to the power of the second argument if that result can in fact be represented exactly as a double value.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › pow
Java Math pow() - Calculate Power | Vultr Docs
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.
🌐
Carmatec
carmatec.com › home › java math.pow() explained: the java power function
Java Math.pow() Explained: The Java Power Function
January 7, 2026 - Math.pow(0.0, 0.0) returns 1.0 (by convention in Java). Any non-zero number raised to exponent 0.0 returns 1.0. Positive zero or negative zero raised to a positive exponent returns zero with the corresponding sign.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › pow
Math.pow() - JavaScript | MDN
July 10, 2025 - A number representing base taken to the power of exponent. Returns NaN in one of the following cases: ... Math.pow() is equivalent to the ** operator, except Math.pow() only accepts numbers.
🌐
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.
🌐
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.
🌐
Dot Net Perls
dotnetperls.com › math-pow-java
Java - Math.pow Method - Dot Net Perls
March 4, 2025 - Let us begin with this simple Java program. We square 5, which gives us 25, and we square 3 which yields 9. We print the results. Result The result of Math.pow is a double. We can cast this to an int if we know that no data loss due to narrowing will occur.
🌐
Quora
quora.com › How-do-we-use-the-pow-function-in-Java
How do we use the pow() function in Java? - Quora
Answer (1 of 5): Java Math.pow() method 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.
🌐
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 ...