When it's a power of 2 keep in mind that you can use a simple and fast shift expression: 1 << exponent

For example:

22 = 1 << 2 = (int) Math.pow(2, 2)
210 = 1 << 10 = (int) Math.pow(2, 10)

For larger exponents (over 31) use long instead:

232 = 1L << 32 = (long) Math.pow(2, 32)

BTW, in Kotlin you have shl instead of <<:

(Java) 1L << 32 = 1L shl 32 (Kotlin)

Answer from Stanislaw Baranski on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.math.pow
Math.Pow(Double, Double) Method (System) | Microsoft Learn
int value = 2; for (int power = 0; power <= 32; power++) Console.WriteLine($"{value}^{power} = {(long)Math.Pow(value, power):N0} (0x{(long)Math.Pow(value, power):X})"); // The example displays the following output: // 2^0 = 1 (0x1) // 2^1 = 2 (0x2) // 2^2 = 4 (0x4) // 2^3 = 8 (0x8) // 2^4 = 16 (0x10) // 2^5 = 32 (0x20) // 2^6 = 64 (0x40) // 2^7 = 128 (0x80) // 2^8 = 256 (0x100) // 2^9 = 512 (0x200) // 2^10 = 1,024 (0x400) // 2^11 = 2,048 (0x800) // 2^12 = 4,096 (0x1000) // 2^13 = 8,192 (0x2000) // 2^14 = 16,384 (0x4000) // 2^15 = 32,768 (0x8000) // 2^16 = 65,536 (0x10000) // 2^17 = 131,072 (0x
🌐
CodeGym
codegym.cc › java blog › java math › math pow() method in java
Math.pow() Method in Java
December 5, 2024 - int answer = (int) Math.pow(5, 4); Now the result is 625. Let’s use fractional numbers for both base and exponent and try to get an answer.
🌐
Programiz
programiz.com › java-programming › library › math › pow
Java Math pow()
In the above example, we have used ... infinity in the program. Note: When we pass an integer value to the pow() method, it automatically converts the int value to the double value....
🌐
Baeldung
baeldung.com › home › java › java numbers › using math.pow in java
Using Math.pow in Java | Baeldung
January 8, 2024 - The method raises a to the power of b and returns the result as double. In other words, a is multiplied by itself b times. ... The output will be 8. Please note that the int casting in the above example is required if we want to have an Integer ...
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › pow
Math.pow() - JavaScript | MDN
Math.pow(base, exponent) base · ... cases: exponent is NaN. base is NaN and exponent is not 0. base is ±1 and exponent is ±Infinity. base < 0 and exponent is not an integer....
🌐
Cppreference
en.cppreference.com › w › cpp › numeric › math › pow
std::pow, std::powf, std::powl - cppreference.com
pow(+0, exp), where exp is a negative odd integer, returns +∞ and raises FE_DIVBYZERO.
🌐
Educative
educative.io › answers › how-to-use-the-mathpow-method-in-java
How to use the Math.pow() method in Java
int ans1 = (int) Math.pow(5,4); System.out.println("5^4 is "+ans1); //Calculating 1.5^3 and storing the returned double result to ans2 · double ans2 = Math.pow(1.5,3); System.out.println("1.5^3 is "+ans2); //Calculating 100^5 and casting the returned double result to int ·
🌐
Octoperf
octoperf.com › blog › 2018 › 03 › 16 › java-math-pow
Java Math.pow Through Code Examples - OctoPerf
The Math.pow method performs an approximation of the result. To get the correct result use the following code: long b = 1; for (int i = 0; i < 15; i++) { b = b * 13; } System.out.println(b);
🌐
Cppreference
cppreference.com › w › c › numeric › math › pow.html
pow, powf, powl - cppreference.com
pow(-0, exponent), where exponent is a negative odd integer, returns -∞ and raises FE_DIVBYZERO
🌐
Javatpoint
javatpoint.com › java-math-pow-method
Math.pow()
Java Math.pow() method with Examples on abs(), min(), max(), avg(), round(), ceil(), floor(), pow(), sqrt(), sin(), cos(), tan(), exp() etc.
🌐
Sololearn
sololearn.com › en › Discuss › 112634 › intmathpowx-y
(int)Math.pow(x, y) | Sololearn: Learn to code for FREE!
@Samuel Neo Thank you for your answer it makes sense now! I did some extra searching with what you have said in mind. So if you want to typecast a double bigger than 2147483647 to an int it goes to the maximum value of the int data type. Because Math.pow(x, y) returns a double that' s what happens.
🌐
CodeAhoy
codeahoy.com › java › Math-Pow-method-JI_11
Java Math.pow() Method with Examples | CodeAhoy
October 26, 2016 - This is a static method like all other methods of the Math class and can be called directly on the Math class as Math.pow(...). ... This method returns a^b or a raised to the power b as a double value. Here’s a simple example where we raise 3 to the power of 2, or 3^2, and convert the result ...
🌐
Dart
api.dart.dev › dart-math › pow.html
pow function - dart:math library - Dart API
For integers, the power is always equal to the mathematical result of x to the power exponent, only limited by the available memory.
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java math.pow() method
Java Math.pow() Method
September 1, 2008 - 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.