Namespaces
Variants
Views
Actions

log2

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
(C99)
log2
(C99)
Power functions
(C99)
(C99)
Trigonometric and hyperbolic functions
(C99)
(C99)
(C99)
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Nearest integer floating point operations
(C99)(C99)(C99)
(C99)
(C99)(C99)(C99)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
float       log2f( float arg );
(since C99)
double      log2( double arg );
(since C99)
long double log2l( long double arg );
(since C99)

Computes the base 2 logarithm of arg.

Contents

[edit] Parameters

arg - floating point value

[edit] Return value

log
2
(arg)

Domain error occurs if arg is negative. NAN is returned in that case.

Range error occurs if arg is 0. -HUGE_VAL is returned in that case.

[edit] Example

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.h>
#include <string.h>
 
#pragma STDC FENV_ACCESS ON
 
void show_fe_exceptions(void)
{
    printf("current exceptions raised: ");
    if(fetestexcept(FE_DIVBYZERO))     printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT))       printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID))       printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW))      printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW))     printf(" FE_UNDERFLOW");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
 
int main(void)
{
    printf("log2(2.0)      = %.1f\n", log2(2.0));
    printf("exp2(1.0)      = %.1f\n", exp2(1.0));        /* inverse of log2 */
    printf("log2(1.0)      = %.1f\n", log2(1.0));
    printf("log2(INFINITY) = %.1f\n", log2(INFINITY));
    printf("log2(NAN)      = %.1f\n", log2(NAN));
    printf("\n");
 
    /* domain error                       */
    /* "invalid" floating-point exception */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("log2(-1.0)     = %f\n", log2(-1.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    /* range and pole errors                     */
    /* "divide-by-zero" floating-point exception */
    /* same for log2(-0.0)                       */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("log2(0.0)      = %f\n", log2(0.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

log2(2.0)      = 1.0
exp2(1.0)      = 2.0
log2(1.0)      = 0.0
log2(INFINITY) = inf
log2(NAN)      = nan
 
log2(-1.0)     = nan
Numerical argument out of domain
current exceptions raised:  FE_INVALID
 
log2(0.0)      = -inf
Numerical result out of range
current exceptions raised:  FE_DIVBYZERO

[edit] See also

computes natural (base e) logarithm (to base e) (ln(x))
(function) [edit]
computes common (base 10) logarithm (log10(x))
(function) [edit]