What is the log10() function in D?

The log10() function in D uses the base 10 to calculate the logarithm of a number. The figure below shows the mathematical representation of the log10() function.

Mathematical representation of the "log10()" function

Note: We need to import std.math in our code, in order to use the log10() function. We can import it like this:
import std.math

Syntax

Let’s look at the syntax for the log10() function.

// number should be float, double or real.
log10(number)

Parameter

This function requires the numberThis number must be greater than 0. for which the logarithm is to be calculated.

Return value

log10() uses the base 10 to return the logarithm of a given number.

Note:

  • If the parameter value is positive infinity, then the log() returns positive infinity.
  • If the parameter value is NaN or negative infinity, then the log() function returns NaN.
  • If the parameter value is -NaN, then the log() function returns -NaN.
  • If the parameter value is <= 0, then the log() function returns negative infinity.

Code example

The code written below shows us how to use the log10() function in D:

import core.stdc.stdio;
import std.stdio;
//header required for function
import std.math;
int main()
{
//integer
writeln ("The value of log10(10) : ",log10(10));
//positive double value
writeln ("The value of log10(2.56) : ",log10(2.56));
//e
writeln ("The value of log10(E) : ",log10(E));
//exceptional outputs
writeln ("The value of log10(-0.0) : ",log10(-0.0));
writeln ("The value of log10(0.0) : ",log10(0.0));
writeln ("The value of log10(real.infinity) : ",log10(real.infinity));
writeln ("The value of log10(-real.infinity) : ",log10(-real.infinity));
writeln ("The value of log10(real.nan) : ",log10(real.nan));
writeln ("The value of log10(-real.nan) : ",log10(-real.nan));
return 0;
}

Code explanation

  • Line 4: We add the header std.math, which is required for the log10() function.

  • Line 9: We calculate the log base 10 of the integer value, using the log10() function.

  • Line 12: We calculate the log base 10 of the positive double value, using the log10() function.

  • Line 15: We calculate the log base 10 of e, using the log10() function.

  • Lines 18–25: We calculate the log base 10 of exceptional numbers, using the log10() function.

Free Resources