YouTip LogoYouTip

C Function Log10

# C Library Function - log10() The `log10()` function is a built-in mathematical function in the C standard library ``. It is used to calculate the common logarithm (base-10 logarithm) of a given floating-point number. This function is widely used in scientific computing, engineering, data analysis, and financial modeling to handle exponential scales and orders of magnitude. --- ## Declaration To use the `log10()` function, you must include the `` header file in your program. The C standard library provides three variations of this function to support different floating-point precisions: ```c #include double log10(double x); float log10f(float x); long double log10l(long double x); ``` ### Parameters * **`x`**: The input floating-point value. This value must be strictly greater than zero ($x > 0$). ### Return Value * Returns the base-10 logarithm of `x` ($\log_{10}(x)$). * If `x` is negative or zero, it will trigger a mathematical domain error or range error. --- ## Error Handling When the input parameter `x` falls outside the valid mathematical domain, the function behaves as follows: * **Domain Error (Negative Input)**: If `x` is negative ($x < 0$), `log10()` returns `NaN` (Not a Number) and sets `errno` to `EDOM`. * **Range Error (Zero Input)**: If `x` is exactly `0`, `log10()` returns `-HUGE_VAL` (negative infinity, `-inf`) and sets `errno` to `ERANGE`. --- ## Code Examples ### Example 1: Basic Usage of `log10()` The following example demonstrates how to calculate the base-10 logarithm of a single positive number. ```c #include #include int main() { double x, ret; x = 10000.0; /* Calculate log10(10000) */ ret = log10(x); printf("log10(%lf) = %lf\n", x, ret); return 0; } ``` #### Output ```text log10(10000.000000) = 4.000000 ``` --- ### Example 2: Handling Multiple Values and Error Checking This comprehensive example demonstrates how to process an array of different values, including edge cases (zero and negative numbers), while safely checking for errors using `errno`. ```c #include #include #include int main() { double values[] = {1.0, 10.0, 100.0, -1.0, 0.0}; int num_values = sizeof(values) / sizeof(values); for (int i = 0; i < num_values; i++) { double x = values; errno = 0; // Reset errno before the function call 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; } ``` #### Output ```text log10(1.000000) = 0.000000 log10(10.000000) = 1.000000 log10(100.000000) = 2.000000 Domain error: log10(-1.000000) is not defined Range error: log10(0.000000) results in an overflow or underflow ``` ### Code Analysis 1. **Array Definition**: We define an array `values` containing positive numbers, a negative number, and zero. 2. **Loop Iteration**: A `for` loop iterates through each element and passes it to `log10()`. 3. **Error Resetting**: `errno` is reset to `0` before each call to ensure we catch errors specific to the current operation. 4. **Error Checking**: * If `errno` equals `EDOM`, a domain error occurred (input was negative). * If `errno` equals `ERANGE`, a range error occurred (input was zero, resulting in negative infinity). * Otherwise, the valid result is printed. --- ## Common Use Cases The `log10()` function is highly versatile and commonly used in the following scenarios: * **Decibel Calculations (dB)**: Used in acoustics and electronics to measure signal-to-noise ratios, power gain, or sound intensity level: $$\text{dB} = 10 \times \log_{10}\left(\frac{P}{P_0}\right)$$ * **pH Scale in Chemistry**: Used to calculate the acidity of a solution: $$\text{pH} = -\log_{10}[\text{H}^+]$$ * **Richter Scale in Seismology**: Used to measure the magnitude of earthquakes. * **Data Normalization**: Compressing highly skewed data distributions in machine learning and statistics to make them easier to visualize and analyze. ## Summary The `log10()` function is the standard tool in C for calculating base-10 logarithms. When implementing this function, always ensure that your inputs are strictly positive, or implement robust error handling using `` to prevent runtime issues such as `NaN` or `-inf` propagation in your calculations.
← C Function ModfC Function Log β†’