What is the logb() function in C++?

Overview

The logb() function in C++ is used to return the logarithm of a given number (|x|) using the FLT_RADIX as the base of the logarithm.

Note:FLT_RADIX is the value of the base of the logarithm. Generally, it is 2, so the logb() is equivalent to log2() for any positive number value.

The logb() function is defined in the <cmath> header file.

Syntax

double logb (double x);
float logb (float x);
long double logb (long double x);
double logb (T x); // For integral type

Parameter value

The logb() function takes a single parameter value:

x: The number whose logarithm is computed.

Return value

The logb() function returns the logarithm of the argument passed to it as its parameter value.

Example

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
double x =100, result;
result = logb(x);
cout << "logb(" << x << ") = " << "log(|" << x << "|) = "<< result << endl;
return 0;
}

Explanation

  • Line 8: We create variables x and result.
  • Line 10: We use the logb() function on the x variable and assign the output to the other variable result.
  • Line 11: We print the result variable.

Free Resources