Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

How to use log10( ) in C++

Educative Answers Team

The log10() function is part of the cmath library in C++ – it allows you to take the logarithm of base 10 of any number.

Logarithm is a math function that finds out x by following the equation below.

y=10xy = 10^{x}

If y is given, we can find x by taking log10(y).

The following table defines the different outputs of the logarithm function.

Input y Output x
y > 1 positive value
y = 1 zero value
0 > y > 1 negative value
y = 0 -\infty
y < 0 not a number

The log10() function in cmath takes a number of any type in the argument and returns its base, 10 log. The returned value can be a float, double, long double or int.

Let’s see different examples of how the log10() function can be used.

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

int main() {
  // input output both of same type
  double x0, y0 = 453.23;
  x0 = log10(y0);
  cout << "Log Base 10 of " << y0 << " is : " << x0 << endl;

  //input output both of different type
  double x1;
  float y1 = 100.45;
  x1 = log10(y1);
  cout << "Log Base 10 of " << y1 << " is : " << x1 << endl;
  
  // output as an integer 
  // notice that the output will discard any decimal places
  int x2;
  long double y2 = 4845645.85;
  x2 = log10(y2);
  cout << "Log Base 10 of " << y2 << " is : " << x2 << endl;

  return 0;
}

RELATED TAGS

c
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring