Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

math.log2

Computes the base-two logarithm of a positive real value.

Version: Python 3.14; repository image, production runtime not attested

Syntax

math.log2(x, /)

Computes the base-two logarithm of a positive real value.

Inputs strictly between zero and one have negative logarithms.

The binary logarithm gives the exponent of two corresponding to the input. Five doublings produce 32, whereas dividing one by two yields 0.5 and an exponent of −1. Its real domain excludes zero and negative arguments, which raise ValueError.

import math
assert math.log2(32) == 5.0 and math.log2(0.5) == -1.0
assert math.log2(1) == 0.0

Examples

import math
print(math.log2(32))
print(math.log2(0.5))

Result

5.0
-1.0

The binary logarithm gives the exponent of two corresponding to the input. Five doublings produce 32, whereas dividing one by two yields 0.5 and an exponent of −1. Its real domain excludes zero and negative arguments, which raise ValueError.

Sources