logb() Function with Example in C++

C++ logb() function: Here, we are going to learn about the logb() function with example of cmath header in C++ programming language.
Submitted by IncludeHelp, on May 20, 2020

C++ logb() function

logb() function is a library function of cmath header, it is used to get the logarithm of |given value|, where logarithm base is FLT_RADIX (On the most of the platforms the FLT_RADIX is 2, thus, logb() function is similar to log2() function for the positive values. It accepts a value (float, double, or long double) and returns the logarithm of the |given value|.

Syntax of logb() function:

C++11:

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

Parameter(s):

  • x – represents the value whose logarithm to be found.

Return value:

It returns the logarithm of the given value |x|.

Note:

  • If the given value is zero, it may cause a domain error or a pole error.

Example:

    Input:
    float x = 2.0f
    
    Function call:
    logb(x);
    
    Output:
    1

    Input:
    float x = -2.0f
    
    Function call:
    logb(x);
    
    Output:
    1

C++ code to demonstrate the example of logb() function

// C++ code to demonstrate the example of
// logb() function

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    float x = 0.0f;

    x = 10.0f;
    cout << "logb(" << x << "): " << logb(x) << endl;

    x = 2.0f;
    cout << "logb(" << x << "): " << logb(x) << endl;

    x = 20.0f;
    cout << "logb(" << x << "): " << logb(x) << endl;

    x = -10.4f;
    cout << "logb(" << x << "): " << logb(x) << endl;

    x = -2.0f;
    cout << "logb(" << x << "): " << logb(x) << endl;

    x = 100.6f;
    cout << "logb(" << x << "): " << logb(x) << endl;

    return 0;
}

Output

logb(10): 3
logb(2): 1
logb(20): 4
logb(-10.4): 3
logb(-2): 1
logb(100.6): 6

Reference: C++ logb() function



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.