C library function - log10()
Description
C Library Function double log10(double x) Return x the common logarithm (logarithm with base 10).
log10() is a function in the C standard library a function in the C standard library used to compute the common logarithm (base 10) of a floating-point number. This is a common mathematical function, widely applied in scientific computing, engineering, and finance.
Declaration
Here is the declaration of the log10() function.
#include
double log10(double x);
float log10f(float x);
long double log10l(long double x);
Parameters
x: the input floating-point number, which must be positive (greater than 0).
Return Value
- Return
xthe common logarithm ofxIf it is negative or zero, a mathematical domain error or range error will occur.
Error handling
- If both the
xIf it is negative,log10()the function returns NaN and setserrnoisEDOM. - If both the
xIf it is zero,log10()the function returns-HUGE_VAL(negative infinity), and setserrnoisERANGE.
Example
The following example demonstrates the use of the log10() function.
Example
#include
#include
int
main
(
)
{
double
x
,
ret
;
x
=
10000
;
/* Compute log10(10000) */
ret
=
log10
(
x
)
;
printf
(
"log10(%lf) = %lf\n"
,
x
,
ret
)
;
return
(
0
)
;
}
Let's 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 logarithms of multiple values:
Example
#include
#include
#include
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's 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 Analysis
- Define an array containing multiple floating-point numbers
values. - Use
for, and loop through each value, calling thelog10(x)Perform the computation. - Reset
errnoto 0 and check for possible errors. - Print the logarithm computation result for each value, or an error message.
Use Cases
log10() The function has a wide range of uses in many applications, including but not limited to:
- Compute common logarithms to handle changes in order of magnitude.
- Analyze data on a logarithmic scale.
- Solve mathematical problems involving common logarithm equations.
- Compute logarithmic returns or logarithmic price changes in finance.
Summary
log10() The function is used to compute the common logarithm of a floating-point number and is an important tool for handling common logarithm operations. By making proper use of log10(), logarithmic operations can be performed in scientific computing, finance, and engineering applications, with possible error conditions handled properly.
C Standard Library -