C library functions -log10()

C standard library - <math.h>

Description

C Library Functionsdouble log10(double x)returnxThe common logarithm (logarithm to base 10) of.

log10()is the C standard library<math.h>A function used to compute the common logarithm (base 10) of a floating-point number. This is a common mathematical function, widely used in fields such as scientific computing, engineering, and finance.

Declaration

Below is the declaration of the log10() function.

#include <math.h>

double log10(double x);
float log10f(float x);
long double log10l(long double x);

Parameter

  • x: the input floating-point number, which must be positive (greater than 0).

Return Value

  • returnxthe common logarithm of, ifxA negative or zero value will cause a mathematical domain error or a range error.

Error Handling

  • ifxis negative,log10()the function will return NaN and seterrnoisEDOM。
  • ifxis zero,log10()the function will return-HUGE_VAL(negative infinity), and seterrnoisERANGE。

Example

The following example demonstrates the usage of the log10() function.

Example

#include <stdio.h>
#include <math.h>

int main ()
{
   double x, ret;
   x = 10000;
 
   /* Calculate log10(10000) */
   ret = log10(x);
   printf("log10(%lf) = %lf\n", x, ret);
   
   return(0);
}

Let us compile and run the above program, which will produce the following result:

log10(10000.000000) = 4.000000

Handling logarithms of multiple values

The following example shows how to handle the logarithms of multiple values:

Example

#include <stdio.h>
#include <math.h>
#include <errno.h>

int main() {
    double values[] = {1.0, 10.0, 100.0, -1.0, 0.0};
    int num_values = sizeof(values) / sizeof(values[0]);

    for (int i = 0; i < num_values; i++) {
        double x = values[i];
        errno = 0;  // Reset errno
        double result = log10(x);

        if (errno == EDOM) {
            printf("Domain error: log10(%f) is not defined\n", x);
        } else if (errno == ERANGE) {
            printf("Range error: log10(%f) results in an overflow or underflow\n", x);
        } else {
            printf("log10(%f) = %f\n", x, result);
        }
    }

    return 0;
}

Let us compile and run the above program, which will produce the following result:

log10(1.000000) = 0.000000
log10(10.000000) = 1.000000
log10(100.000000) = 2.000000
log10(-1.000000) = nan
log10(0.000000) = -inf

Code explanation

  • Define an array containing multiple floating-point numbersvalues。
  • UsageforLoop through each value, callinglog10(x)perform the calculation.
  • reseterrnoto 0 and check for possible errors.
  • Print the logarithm calculation result for each value, or an error message.

Use Case

log10()The function has broad applications in many fields, including but not limited to:

  • Computing common logarithms to handle changes in orders of magnitude.
  • Analyze data on a logarithmic scale.
  • Solving mathematical problems involving common logarithm equations.
  • Computing logarithmic returns or logarithmic price changes in finance.

Summary

log10()The function is used to compute the common logarithm of floating-point numbers and is an important tool for handling common logarithm operations. Through reasonable uselog10(), logarithmic operations can be implemented in scientific computing, financial, and engineering applications, while ensuring that possible error cases are properly handled.

C standard library - <math.h>

other extensions