English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ How to use logb() function and examples

C++ Library Function <cmath>

C ++The logb() function in uses FLT_RADIX as the base of the logarithm and returns the logarithm of |x|.

Generally, FLT_RADIX is2,so for positive values, logb() is equivalent tolog2().

The function is in<cmath>header file is defined.

logb() prototype [from C ++ 11Standard begins]

double logb(double x);
float logb(float x);
long double logb(long double x);
double logb(T x); //For integer types

The logb() function has only one parameter and returns a value of type double, float, or long double.

logb() parameter

The ilogb() function takes one parameter, whose logb is calculated.

logb() return value

The logb() function uses FLT_RADIX as the base of the logarithm and returns the logarithm of |x|.

If x is zero, it may cause domain error, pole error, or no error, depending on the library implementation.

Example1:The logb() function in C ++How does it work?

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

When running the program, the output is:

logb(121.056) = log(|121.056|) = 6

Example2:An integer type logb() function

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

When running the program, the output is:

logb(-5) = log(|-5|) = 2

  C++ Library Function <cmath>