Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / Python

math.log2

Restituisce l’esponente della potenza di due corrispondente a x.

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

Sintassi

math.log2(x, /)

Restituisce l’esponente della potenza di due corrispondente a x.

Poiché 2⁵ = 32, la prima chiamata dà 5,0. Un mezzo è il reciproco di due, quindi 2⁻¹ = 0,5 dà −1,0.

Zero e i numeri negativi sollevano ValueError.

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

Esempi

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

Risultato

5.0
-1.0

Zero e i numeri negativi sollevano ValueError.

Fonti