logb(), logbf(), logbl() — Unbiased exponent

Standards

Standards / Extensions C or C++ Dependencies

XPG4.2
C99
Single UNIX Specification, Version 3
C++ TR1 C99

both z/OS® V1R7 for logbf(), logbl()

Format

#define _XOPEN_SOURCE_EXTENDED 1
#include <math.h>

double logb(double x);
C99:
#define _ISOC99_SOURCE
#include <math.h>

float logbf(float x);
long double logbl(long double x);
C++ TR1 C99:
#define _TR1_C99
#include <math.h>

float logb(float x); 
long double logb(long double x);

General description

Returns the exponent of its argument x, as a signed integer value in floating-point mode. If x is subnormal, it is treated as a normalized number.
Note: The following table shows the viable formats for these functions. See IEEE binary floating-point for more information about IEEE Binary Floating-Point.
Function Hex IEEE
logb X X
logbf X X
logbl X X

Returned value

If successful, logb() returns the exponent of x.

logb() will fail under the following condition: If x is equal to 0.0, logb() will return -HUGE_VAL and set errno to EDOM.

Example

/*
 * This program illustrates the use of logb() function
 *
 */
#define _ISOC99_SOURCE
#include <math.h>
#include <float.h>  /* Needed for FLT_RADIX */
#include <stdio.h>


void main() {

  int i;
  union {
    double number;
    unsigned char  uchars [sizeof(double)];
  } dblval;
  double logbx;

  printf("Illustrates the logb() function");

  #ifdef __BFP__
    printf(" (IEEE version)\n\n");
  #else
    printf(" (HFP version)\n\n");
  #endif

  /* generate the smallest possible double number */
  for (i=0; i<sizeof(double); i++)
    dblval.uchars[i] = 0;
  dblval.uchars[1] = 0x10;

  logbx = logb(dblval.number);

  printf("x = %g\n",dblval.number);
  printf("logb(x) = %f\n\n", logbx);

  printf("pow(FLT_RADIX, logb(x) ) should equal x\n");
  printf("pow(%d,%f) = %g\n",FLT_RADIX, logbx, pow(FLT_RADIX, logbx));
}

Output

Illustrates the logb() function (IEEE version)

x = 2.22507e-308
logb(x) = -1022.000000

pow(FLT_RADIX, logb(x) ) should equal x
pow(2,-1022.000000) = 2.22507e-308

Related information