C++ cmath log10() function
Example
Return the base 10 logarithm of different numbers:
cout << log10(120.0f);
cout << log10(10.0f);
cout << log10(3.1623);
cout << log10(1.0);
cout << log10(0.0f);
cout << log10(-1.0f);
Try it Yourself »
Definition and Usage
The log10()
function returns the base 10 logarithm of a number.
Tip: The base 10 logarithm indicates approximately how many digits the integer part of a number has. See how to use it to count digits in the example below.
The log10()
function is defined in the <cmath>
header file.
Syntax
One of the following:
log10(double number);
log10(float number);
Parameter Values
Parameter | Description |
---|---|
number |
Required. Specifies the value to calculate the logarithm for. If the value is negative, it returns NaN (Not a Number). If the value is 0, it returns -infinity. If this is an integer type then it will be treated as a double .
|
Technical Details
Returns: | A float value (if the argument is float) or double value (in any other case) representing the base 10 logarithm of a number. |
---|
More Examples
Example
Count the digits in a number:
double x = 1260.0;
int digits = floor( log10( x ) ) + 1;
cout << "The number " << x << " has " << digits << " digits.";