pow
pow(int|float $num, int|float $exponent): int|float
Raises a number to a power (PHP 8 recommends the ** operator).
C C23
C23
#include <math.h>
#include <stdio.h>
int main(void) {
printf("%g\n", pow(2, 8)); // %g drops trailing zeros
printf("%g\n", pow(2, 10)); // integer exponent
printf("%.4f\n", pow(2, 0.5)); // float exponent: sqrt(2)
return 0;
}
Output
256 1024 1.4142
C++ C++26
C++26
#include <cmath>
#include <iostream>
int main() {
std::cout << (int)std::pow(2, 8) << "\n";
std::cout << (int)std::pow(2, 10) << "\n"; // pow() called directly, int exponent
return 0;
}
Output
256 1024
C# v11.0.100
v11.0.100
Console.WriteLine(Math.Pow(2, 8));
Console.WriteLine(Math.Pow(2, 10)); // Math.Pow returns double, prints 1024
Console.WriteLine(Math.Round(Math.Pow(2, 0.5), 4)); // float exponent: sqrt(2)
Output
256 1024 1.4142
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Pow(2, 8))
fmt.Println(math.Pow(2, 10)) // float64: prints 1024
fmt.Println(math.Round(math.Pow(2, 0.5)*10000) / 10000) // float exponent: sqrt(2)
}
Output
256 1024 1.4142
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
System.out.println((long) Math.pow(2, 8));
System.out.println((long) Math.pow(2, 10));
System.out.println(Math.round(Math.pow(2, 0.5) * 10000) / 10000.0); // float exponent
}
}
Output
256 1024 1.4142
JavaScript v26.13.1
v26.13.1
console.log(2 ** 8)
console.log(2 ** 10, Math.round(2 ** 0.5 * 10000) / 10000) // float exponent: sqrt(2)
Output
256 1024 1.4142
PHP v8.6.0
v8.6.0
echo 2 ** 8, "\n";
// the function call itself: pow(base, exp); float exponents work too
echo pow(2, 10), "
";
echo round(pow(2, 0.5), 4);
Output
256 1024 1.4142
Python v3.15.2
v3.15.2
print(2 ** 8)
print(2 ** 10, round(2 ** 0.5, 4)) # float exponent: sqrt(2)
Output
256 1024 1.4142
Rust v1.99.1
v1.99.1
fn main() {
println!("{}", 2u32.pow(8));
println!("{}", 2u32.pow(10)); // integer exponent
println!("{:.4}", 2.0_f64.powf(0.5)); // float exponent: sqrt(2)
}
Output
256 1024 1.4142
TypeScript v26.13.1
v26.13.1
console.log(2 ** 8);
console.log(2 ** 10, Math.round(2 ** 0.5 * 10000) / 10000); // float exponent: sqrt(2)
Output
256 1024 1.4142
Language differences
C: computes with pow(); %g drops redundant zeros.
Go: math.Pow returns a float.
Java: Math.pow returns a float.
JavaScript: uses the ** operator.
PHP: uses the ** operator (recommended since PHP 8; the pow() function still works).
Python: uses the ** operator.
Rust: the integer .pow() returns an integer.