Namespaces
Variants

log2, log2f, log2l

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
log2
(C99)
(C99) (C23)
(C23)
(C23)
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
Definido en el encabezado <math.h>
float log2f ( float arg ) ;
(1) (desde C99)
double log2 ( double arg ) ;
(2) (desde C99)
long double log2l ( long double arg ) ;
(3) (desde C99)
Definido en el encabezado <tgmath.h>
#define log2( arg )
(4) (desde C99)
1-3) Calcula el logaritmo en base 2 de arg .
4) Macro genérico de tipos: Si arg tiene tipo long double , log2l es llamado. De lo contrario, si arg tiene tipo entero o el tipo double , log2 es llamado. De lo contrario, log2f es llamado.

Contenidos

Parámetros

arg - valor de punto flotante

Valor de retorno

Si no ocurren errores, se devuelve el logaritmo en base 2 de arg ( log 2 (arg) o lb(arg) ).

Si ocurre un error de dominio, se devuelve un valor definido por la implementación (NaN donde esté soportado).

Si ocurre un error de polo, -HUGE_VAL , -HUGE_VALF , o -HUGE_VALL es devuelto.

Manejo de errores

Los errores se reportan como se especifica en math_errhandling .

Error de dominio ocurre si arg es menor que cero.

Puede ocurrir un error de polo si arg es cero.

Si la implementación soporta aritmética de punto flotante IEEE (IEC 60559),

  • Si el argumento es ±0, se retorna -∞ y FE_DIVBYZERO se activa.
  • Si el argumento es 1, se retorna +0
  • Si el argumento es negativo, se retorna NaN y FE_INVALID se activa.
  • Si el argumento es +∞, se retorna +∞
  • Si el argumento es NaN, se retorna NaN

Notas

Para un entero arg , el logaritmo binario puede interpretarse como el índice basado en cero del bit más significativo en 1 en la entrada.

Ejemplo

#include <stdio.h>
#include <math.h>
#include <float.h>
#include <errno.h>
#include <fenv.h>
// #pragma STDC FENV_ACCESS ON
int main(void)
{
    printf("log2(65536) = %f\n", log2(65536));
    printf("log2(0.125) = %f\n", log2(0.125));
    printf("log2(0x020f) = %f (highest set bit is in position 9)\n", log2(0x020f));
    printf("base-5 logarithm of 125 = %f\n", log2(125)/log2(5));
    // special values
    printf("log2(1) = %f\n", log2(1));
    printf("log2(+Inf) = %f\n", log2(INFINITY));
    //error handling
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("log2(0) = %f\n", log2(0));
    if(errno == ERANGE) perror("    errno == ERANGE");
    if(fetestexcept(FE_DIVBYZERO)) puts("    FE_DIVBYZERO raised");
}

Salida posible:

log2(65536) = 16.000000
log2(0.125) = -3.000000
log2(0x020f) = 9.041659 (highest set bit is in position 9)
base-5 logarithm of 125 = 3.000000
log2(1) = 0.000000
log2(+Inf) = inf
log2(0) = -inf
    errno == ERANGE: Numerical result out of range
    FE_DIVBYZERO raised

Referencias

  • Estándar C17 (ISO/IEC 9899:2018):
  • 7.12.6.10 Las funciones log2 (p: 179)
  • 7.25 Matemáticas genéricas de tipos <tgmath.h> (p: 272-273)
  • F.10.3.10 Las funciones log2 (p: 381)
  • Estándar C11 (ISO/IEC 9899:2011):
  • 7.12.6.10 Las funciones log2 (p: 246)
  • 7.25 Matemáticas genéricas de tipos <tgmath.h> (p: 373-375)
  • F.10.3.10 Las funciones log2 (p: 522)
  • Estándar C99 (ISO/IEC 9899:1999):
  • 7.12.6.10 Las funciones log2 (p: 226)
  • 7.22 Matemáticas genéricas de tipo <tgmath.h> (p: 335-337)
  • F.9.3.10 Las funciones log2 (p: 459)

Véase también

(C99) (C99)
calcula el logaritmo natural (base e ) ( ln(x) )
(función)
calcula el logaritmo común (base 10 ) ( log 10 (x) )
(función)
(C99) (C99) (C99)
calcula el logaritmo natural (base e ) de 1 más el número dado ( ln(1+x) )
(función)
(C99) (C99) (C99)
calcula 2 elevado a la potencia dada ( 2 x )
(función)