log10() Function with Example in C++

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

C++ log10() function

log10() function is a library function of cmath header, it is used to get the common logarithm (the base-10 logarithm) of the given value. It accepts a value (float, double, or long double) and returns the common logarithm.

Syntax of log10() function:

C++11:

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

Parameter(s):

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

Return value:

It returns the common logarithm of the given value x.

Note:

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

Example:

    Input:
    float x = 10.0f
    
    Function call:
    log(x);
    
    Output:
    1

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

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

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

int main()
{
    float x = 0.0f;

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

    x = 1.0f;
    cout << "log10(" << x << "): " << log10(x) << endl;

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

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

    x = 0.0f;
    cout << "log10(" << x << "): " << log10(x) << endl;

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

    return 0;
}

Output

log10(10): 1
log10(1): 0
log10(20): 1.30103
log10(-10.4): nan
log10(0): -inf
log10(100.6): 2.0026

Reference: C++ log10() function



Comments and Discussions!

Load comments ↻





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