Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C library function - log10()

C library function - log10()

C Standard Library - <math.h> C Standard Library -

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

Return Value

Error handling

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

Use Cases

log10() The function has a wide range of uses in many applications, including but not limited to:

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 - <math.h> C Standard Library -