log10() Function

The log10() function computes the common (base-10) logarithm of a value. It is part of the math.h library and is widely used in various mathematical computations, particularly where logarithmic conversions are needed. The function handles error conditions such as domain and pole errors, setting appropriate error indicators when necessary.


Syntax of log10()

</>
Copy
double log10(double x);
float  log10f(float x);
long double log10l(long double x);

Parameters

ParameterDescription
xThe value for which the common logarithm is calculated. If x is negative, a domain error occurs. If x is zero, a pole error may occur, depending on the library implementation.

Note: When a domain error occurs (x is negative), the global variable errno may be set to EDOM or the floating-point exception FE_INVALID may be raised. Similarly, when a pole error occurs (x is zero), errno may be set to ERANGE or the exception FE_DIVBYZERO may be raised.


Examples for log10()

Example 1: Calculating the Logarithm of a Positive Number

This example demonstrates how to use log10() to compute the logarithm of a positive value.

Program

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

int main() {
    double value = 1000.0;
    double result = log10(value);

    printf("The common logarithm of %.2f is %.2f\n", value, result);
    return 0;
}

Explanation:

  1. A variable value is initialized with 1000.0.
  2. The log10() function is called to compute its common logarithm.
  3. The result is stored and then printed using printf().

Program Output:

The common logarithm of 1000.00 is 3.00

Example 2: Handling a Domain Error with a Negative Input

This example shows how to handle a domain error when log10() is used with a negative value.

Program

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

int main() {
    double value = -10.0;
    errno = 0; // Reset errno before calling log10
    double result = log10(value);

    if (errno == EDOM) {
        printf("Domain error: Cannot compute log10 for a negative number.\n");
    } else {
        printf("The common logarithm of %.2f is %.2f\n", value, result);
    }
    return 0;
}

Explanation:

  1. The variable value is set to -10.0, which is negative.
  2. errno is reset to zero before calling log10().
  3. The function call triggers a domain error, and errno is set to EDOM.
  4. The program checks errno and prints an appropriate error message.

Program Output:

Domain error: Cannot compute log10 for a negative number.