Python math.exp2() Function
In scientific computing and computer science,powers of 2(powers of 2) are one of the most fundamental exponential operations. Whether analyzing algorithmic complexity, cryptography, or computer storage units, computing powers of 2 is indispensable.
math.exp2() is a function introduced in Python 3.11, designed specifically to compute 2 to the power of x, that is, 2^x.
Word meaning: exp2 is an abbreviation of "exponential base 2", meaning "an exponent with base 2".
Basic syntax and parameters
math.exp2() is a static function of the math module, so the math module must be imported before use.
Syntax format
import math
math.exp2(x)
Parameter Description
- Parameters:
x - Type: numeric (integer or floating-point)
- Description: the exponent, indicating which power of 2 is taken.
Return Value
- Returns 2 to the power of x, that is, 2^x.
- The return type is a floating-point number.
Example
Let's master it thoroughly through examples math.exp2() through a series of examples from simple to complex.
Example 1: basic usage - computing integer powers of 2
Example
import
math
Compute integer powers of 2
(
"2 to the power of 0:"
,
math
.
exp2
(
0
)
)
1
(
"2 to the power of 1:"
,
math
.
exp2
(
1
)
)
2
(
"2 to the power of 2:"
,
math
.
exp2
(
2
)
)
4
(
"2 to the power of 3:"
,
math
.
exp2
(
3
)
)
8
(
"2 to the power of 4:"
,
math
.
exp2
(
4
)
)
16
(
"2 to the power of 5:"
,
math
.
exp2
(
5
)
)
32
(
"2 to the power of 10:"
,
math
.
exp2
(
10
)
)
1024
Negative integer powers
(
"Negative integer powers:"
)
(
"2 to the power of -1:"
,
math
.
exp2
(
1
)
)
0.5
(
"2 to the power of -2:"
,
math
.
exp2
(
2
)
)
0.25
(
"2 to the power of -3:"
,
math
.
exp2
(
3
)
)
0.125
Result:
2 的 0 次方: 1.0
2 的 1 次方: 2.0
2 的 2 次方: 4.0
2 的 3 次方: 8.0
2 的 4 次方: 16.0
2 的 5 次方: 32.0
2 的 10 次方: 1024.0
Negative integer power:
2 的 -1 次方: 0.5
2 的 -2 次方: 0.25
2 的 -3 次方: 0.125
Example 2: floating-point exponents
math.exp2() Floating-point exponents are also supported, which is very useful in scientific computing.
Example
import
math
Compute floating-point powers of 2
(
"2 to the power of 0.5:"
,
math
.
exp2
(
0.5
)
)
√2 ≈ 1.414
(
"2 to the power of 1.5:"
,
math
.
exp2
(
1.5
)
)
2√2 ≈ 2.828
(
"2 to the power of 2.5:"
,
math
.
exp2
(
2.5
)
)
4√2 ≈ 5.657
Verify with Python built-ins
import
math
(
"Verification (2^x = 2 to the power of x):"
)
for
x
in
[
0
,
0.5
,
1
,
1.5
,
2
,
3
]
:
(
f
"math.exp2({x}) = {math.exp2(x)}, 2**{x} = {2**x}"
)
Result:
2 的 0.5 次方: 1.4142135623730951
2 的 1.5 次方: 2.8284271247461903
2 的 2.5 次方: 5.656854249492381
验证:
math.exp2(0.5) = 1.4142135623730951, 2**0.5 = 1.4142135623730951
math exp2(1) = 2.0, 2**1 = 2
math.exp2(1.5) = 2.8284271247461903, 2**1.5 = 2.8284271247461903
math.exp2(2) = 4.0, 2**2 = 4
math.exp2(3) = 8.0, 2**3 = 8
Example 3: practical application - computer storage units
Powers of 2 are used in computing to calculate storage capacity.
Example
import
math
Computer storage units (based on powers of 2)
(
"=== Computer storage units ==="
)
units
=
[
(
0
,
"B (byte)"
)
,
(
10
,
"KB (kilobyte)"
)
,
(
20
,
"MB (megabyte)"
)
,
(
30
,
"GB (gigabyte)"
)
,
(
40
,
"TB (terabyte)"
)
,
(
50
,
"PB (petabyte)"
)
]
for
exp
,
unit_name
in
units:
bytes_val
=
math
.
exp2
(
exp
)
if
exp
=
40
:
(
f
"1 {unit_name}: {bytes_val:.2e} bytes"
)
else
:
(
f
"1 {unit_name}: {int(bytes_val)} bytes"
)
Given a file size, find the corresponding unit
file_size
=
1073741824
1GB = 2^30
(
f
"File size {file_size} bytes = {file_size / math.exp2(30):.2f} GB"
)
Result:
=== 计算机存储单位 ===
1 B (字节): 1 字节
1 KB (千字节): 1024 字节
1 MB (兆字节): 1048576 字节
1 GB (吉字节): 1073741824 字节
1 TB (太字节): 1099511627776 字节
1 PB (拍字节): 1.12e+15 字节
文件大小 1073741824 字节 = 1.00 GB
Example 4: algorithmic complexity analysis
Powers of 2 are very common in algorithm analysis for describing exponential growth.
Example
import
math
Demonstrate exponential growth
(
"=== Exponential growth (2^n) ==="
)
for
n
in
range
(
1
,
11
)
:
result
=
math
.
exp2
(
n
)
Visualize growth with *
stars
=
"*"
min
(
int
(
result /
math
.
exp2
(
4
)
)
,
50
)
(
f
"n={n:2d}: {int(result):6d} {stars}"
)
Logarithmic complexity: log2(n)
(
"=== Logarithmic complexity (log2 n) ==="
)
for
n
in
[
1
,
2
,
4
,
8
,
16
,
32
,
64
,
128
,
256
,
1024
]
:
log_val
=
math
.
log2
(
n
)
(
f
"log2({n:4d}) = {log_val:.2f}"
)
Result:
=== 指数级增长 (2^n) ===
n= 1: 2
n= 2: 4
n= 3: 8 **
n= 4: 16 ***
n= 5: 32 ******
n= 、物理 6: 64 ************
n= 7: 128 *************************
n= 8: 256 ******************************************
n= 9: 512 ************************************************************
n=10: 1024 *******************************************************************
=== 对数复杂度 (log2 n) ===
关系 log2( 1) = 0.00
log2( 2) = 1.00
log2( 4) = 2.00
log3( 8) = 3.任意值 0
log2( 16) = 3.00
log2( 32) = 4.00
密码学应用
Example 5: cryptography and network security
In cryptography, powers of 2 are used to compute key space size.
Example
import
math
Key space size
(
"=== Key space size ==="
)
key_lengths
=
[
64
,
128
,
256
,
512
]
for
bits
in
key_lengths:
key_space
=
math
.
exp2
(
bits
)
(
f
"{bits}-bit key: {key_space:.2e} possibilities"
)
Security strength comparison
(
"=== Security strength comparison ==="
)
Assume 1 quintillion (10^18) attempts per second are possible
attempts_per_second
=
10
**
18
seconds_per_year
=
365
24
3600
for
bits
in
[
64
,
128
,
256
]
:
key_space
=
math
.
exp2
(
bits
)
years_to_crack
=
key_space /
(
attempts_per_second * seconds_per_year
)
(
f
"{bits}-bit key: takes {years_to_crack:.2e} years to crack"
)
Result:
Python math Module