C Programming

Log() Function in C Language

The mathematical library of the C language contains a complete set of functions designed to solve the various mathematical problems. These functions make this language a powerful tool to compile the programs aimed at the scientific field in different areas.

To solve the logarithm calculations, this library provides two basic functions. The log() function is to find the natural logarithm of a variable. While the log10() function is to find the logarithm of a variable in base 10.

In this Linux Hint article, we will explain the step-by-step process on how to calculate the natural logarithm of a variable using the log() function of the C language math library.

We will give a theoretical overview of this function, its input and output arguments, and the respective data type. Then, we will see the step-by-step process on how to implement this function correctly with a practical example that includes the code fragments and images.

Log() Function Syntax in C Language

double log (double x);

Log() Function Description in C Language

The log() function returns the natural logarithm of the variable x. For base 10 logarithm calculations, the C math library provides the log10() function which has the same syntax and calling method as the log() function.

If the variable x whose natural logarithm is to be determined contains a negative value or is equal to 0, this function returns an ERRANGE error.

The log() function is part of C’s mathematical library. Its use must be defined beforehand in our “.c” code. Otherwise, it is defined in the “.h” header with the following declaration:

#include <math.h>

Once the “math.h” header is included in our code. We can now use the log() function and the complementary log10() and other functions provided by the C math library.

How to Find the Natural Logarithm of a Variable with the Log() Function in the C Language

In this example, we will show you the step-by-step process on how to include the libraries, declare the required variables, and find the natural logarithm of the variable x using the log() function of the mathematical library of the C language.

The first step is to include the headers of the libraries that we need for compilation. In this case, it is the header of the standard input/output library which is the “stdio.h” and the header of the C math library, “math.h”.

#include <stdio.h>

#include <math.h>

void main ()

 {

  //…

 }

We then define the necessary input and output variables for the function, both of double type.

These variables are “x” for the input argument which stores the value from which we want to obtain the natural logarithm and the “r” variable which is the output argument to which the log() returns the result of the operation.

In this example, we want to get the logarithm of 2.2. We assign this value to the x variable.

#include <stdio.h>

#include <math.h>

void main ()
  {
double x = 2.2;
double r;
  }

Finally, we call the log() function and pass the “x” variable as input argument and the “r” variable as output argument.

We display the result of this operation on the screen via the command console using the printf() function.

#include <stdio.h>

#include <math.h>

void main ()
  {
double x = 2.2;
double r;
   r = log ( x );
   printf("The natural logarithm of x is: %f\n", r);
  }

In the following figure, we see the result of compiling and executing this code with the result of the natural logarithm of 2.2 output to the command console:

 

Errors and Warnings in the Use of the Log() Function in C Language

When we make use of this function, the compiler may throw the following warning: ” undefined reference to `log’ “.

This is because in the versions after C99, the mathematical library is decoupled from the C standard library.

The most practical solution is to call the mathematical library with “-lm” on the command line of the same build as shown in the following:

~$ gcc Documents/example.c -o example -lm

The following image shows the correct way to call the math library in the build command line. As we can see, the build output does not give the warning that we previously saw.

If the x variable whose natural logarithm is to be determined contains a value which is equal to 0, this function generates the ERANGE or “out of range” error.

Conclusion

In this Linux Hint article about the C language, we explained the step-by-step process on how to properly use the log() function to obtain the natural logarithm of a variable. We showed you the syntax of this function, its input and output arguments, and taught you how to include the necessary headers to use it. We also pointed out some warning messages and the most common errors when using this function. We also showed you how to correctly compile the code that uses the log() function in gcc. We hope that this article is useful for you. You can find more articles like this one on the language in Linux Hint search engine.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).