Python math.exp2() function

Python math 模块Python math module


In the fields of scientific computing and computers,Power of 2(Powers of 2) are one of the most fundamental exponential operations. Whether in algorithm complexity analysis, cryptography, or computer storage units, calculations involving powers of 2 are indispensable.

math.exp2()It is a function introduced in Python 3.11, specifically used for calculation.2 to the power of x, that is, 2ˣ.

Word definition: exp2It is the abbreviation of "exponential base 2", meaning "exponent with base 2".


Basic syntax and parameters

math.exp2()It is a static function of the math module, and you need to import the math module before using it.

grammatical format

import math

math.exp2(x)

Parameter Description

  • parameter: x
    • Type: Numeric (integer or floating point)
    • Description: Exponent, indicating the power of 2.

Return value

  • The return type is a floating-point number.

Instance

Let's thoroughly master it through examples.math.exp2()Usage.

Example 1: Basic usage - calculating integer powers of 2

Example

import math

Calculate integer powers of 2.
print(2 to the power of 0:, math.exp2(0))   # 1
print(2 to the 1st power:, math.exp2(1))   # 2
print(2 to the power of 2:, math.exp2(2))   # 4
print(2 to the power of 3:, math.exp2(3))   # 8
print(2 to the 4th power:, math.exp2(4))   # 16
print(2 to the 5th power:, math.exp2(5))   # 32
print(2 to the 10th power:, math.exp2(10))  # 1024

Negative integer power
print("\nNegative integer power:)
print(2 to the power of -1:, math.exp2(-1))  # 0.5
print(2 to the power of -2:, math.exp2(-2))  # 0.25
print(2 to the power of -3:, math.exp2(-3))  # 0.125

Execution 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

负整数次幂:
2 的 -1 次方: 0.5
2 的 -2 次方: 0.25
2 的 -3 次方: 0.125

Example 2: Floating-Point Power

math.exp2()It also supports floating-point powers, which is very useful in scientific computing.

Example

import math

# Calculate 2 raised to a floating-point power
print(2 to the power of 0.5:, math.exp2(0.5))    # √2 ≈ 1.414
print(2 to the 1.5th power:, math.exp2(1.5))    # 2√2 ≈ 2.828
print(2 to the power of 2.5:, math.exp2(2.5))    # 4√2 ≈ 5.657

Use Python built-in validation.
import math
print("\nVerify (2^x = 2 to the power of x):)
for x in [0, 0.5, 1, 1.5, 2, 3]:
    print(f"math.exp2({x}) = {math.exp2(x)}, 2**{x} = {2**x}")

Execution 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 two are used in computers to calculate storage capacity.

Example

import math

# Computer storage units (based on powers of 2)
print(=== 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:
        print(f1 {unit_name}: {bytes_val:.2e} bytes)
    else:
        print(f1 {unit_name}: {int(bytes_val)} bytes)

# Given the file size, find the corresponding unit.
file_size = 1073741824  # 1GB = 2^30
print(f"\nFile size {file_size} bytes = {file_size / math.exp2(30):.2f} GB)

Execution 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: Algorithm Complexity Analysis

Powers of 2 are very common in algorithm analysis, used to describe exponential growth.

Example

import math

Show exponential growth.
print(=== 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)
    print(f"n={n:2d}: {int(result):6d} {stars}")

Logarithmic complexity: log2(n)
print("\nLogarithmic complexity (log2 n))
for n in [1, 2, 4, 8, 16, 32, 64, 128, 256, 1024]:
    log_val = math.log2(n)
    print(f"log2({n:4d}) = {log_val:.2f}")

Execution 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 calculate the size of the key space.

Example

import math

Key space size
print(=== Key space size ===)
key_lengths = [64, 128, 256, 512]

for bits in key_lengths:
    key_space = math.exp2(bits)
    print(f"{bits}-bit key: {key_space:.2e} possibilities")

# Security Strength Comparison
print("\nSecurity strength comparison)
Assume that 10^18 attempts can be tried per second.
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)
    print(f"{bits}-bit key: requires {years_to_crack:.2e} years to crack")

Running result: Other extensions