Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++

What are multiple examples of log10() in C++?

Rukhshan Haroon

The log10 function takes in one argument of any numerical data-type and returns its base 10 or common logarithm.

To use the log10 function, the cmath header file needs to be included in the program:

#include <cmath>

Parameters

The function takes in one parameter of any numerical data-type supported by C++. The following is a non-exhaustive list of valid data-types for the function parameter:

  • float
  • double
  • integer

The value of the function parameter must be greater than 0; otherwise, the function will return NaN.

Return value

log10 returns a value of data-type double. The data-type of the input parameter does not affect the return value’s data-type.

The return value depends on the input parameters. This is shown in the following example.

Examples

The code below shows that if the input parameter is greater than 1, the return value is positive:

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

int main ()
{
	int input = 2;
	double output;

	output = log10(input);
	cout << "The base-10 logarithm of " << input << " is " <<output<<"." <<endl;
	
	return 0;
}

The following code shows that if the input parameter is equal to 1, the return value is 0:

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

int main ()
{
	int input = 1;
	double output;

	output = log10(input);
	cout << "The base-10 logarithm of " << input << " is " <<output<<"." <<endl;
	
	return 0;
}

Here, the code shows that if the input parameter is greater than 0 but smaller than one, the return value is negative:

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

int main ()
{
	double input = 0.5;
	double output;

	output = log10(input);
	cout << "The base-10 logarithm of " << input << " is " <<output<<"." <<endl;
	
	return 0;
}

The code below shows that if the input parameter is 0, the return value is negative infinity:

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

int main ()
{
	int input = 0;
	double output;

	output = log10(input);
	cout << "The base-10 logarithm of " << input << " is " <<output<<"." <<endl;
	
	return 0;
}

The code below shows that if the input parameter is smaller than 0, the return value is NaN:

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

int main ()
{
	int input = -1;
	double output;

	output = log10(input);
	cout << "The base-10 logarithm of " << input << " is " <<output<<"." <<endl;
	
	return 0;
}

RELATED TAGS

c++

CONTRIBUTOR

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

View all Courses

Keep Exploring