C Programming

fmod() Function in C Language

The basic Mathematical operations in the C language, as in most programming languages, are performed explicitly within the code using the operator corresponding to the desired computation. However, for some calculations, even if they are simple, we must use functions from the standard or Mathematical libraries of this language to solve them.

In C, functions are designed not only to solve the specific Mathematical problema. But also, the data type of the variable we use for the operation. For example, the mod() function determines the remainder after a division of doubles, while the fmod() function determines the remainder after a division of floats.

In this Linux Hint article, we will explain how to use the fmod() function to find the remainder after a floating point division.

We will give a theoretical explanation of this function, its input and output arguments and the data types each of them accepts. Then, we will use it in practical examples that we have prepared for you with code fragments and images, explaining step by step how to use the fmod() function correctly in the C language.

Syntax of the fmod() Function in C Language

double system ( double a, double b );

Description of the fmod() Function in C Language

The fmod() function determines the remainder or modulus after a division of floating point double numbers and returns the result in the same format.

This function is complementary to the mod() function and differs only by data type in its input and output arguments. While one function determines the modulus of an integer double division, the other does so with floating point doubles.

The fmod() function is part of C math library, so its use must be defined beforehand in our “.c” code or otherwise, 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 fmod() function and the complementary provided by the C math library.

How to Get the Remainder or Modulus After a Floating Point Division with the fmod() Function in the C Language.

In this example we show you how to include the libraries, define the variables and get the floating point modulus after a division with the fmod() function.

The first step is to include the libraries that we will use. This function belongs to the Math library. So, we will include “math.h” and the C standard input/output library.

#include <stdio.h>

#include <math.h>

void main ()
  {
  //…
  }

Then, we define the doubles “a” and “b” which will be the divisor and the dividend. The double “r”, which will be the output argument in which we will store the result. We will assign fraction values to the variables “a” and “b”.

#include <stdio.h>

#include <math.h>

void main ()
  {
double a = 11.5756789;
double b = 3.23456789;
double r;
  }

As soon as the variables are defined with their corresponding values, we call the function fmod() and pass as input argument. The dividend “a” and the divisor “b” is separated by commas.

The output argument is the variable “r”. Then, we display its contents or the result of the operation in the command console using the printf() function.

#include <stdio.h>

#include <math.h>

void main ()
  {
double a = 11.5756789;
double b = 3.23456789;
double r;

   r = fmod( a, b );
   printf( "The modulus of a/b is: %f\n", r );
  }

The image below shows the result of this code. As you can see, fmod() returns the modulus of the division of a/b in floating point.

Warning ” undefined reference to `fmod’ ” in the Code Compilation that Use fmod() Function.

When we make use of this function, the compiler may throw the following warning:

” undefined reference to `fmod’ ”

This is because in versions after C99 the Mathematical library has been decoupled from the C standard library.

The most practical solution is to call the correct library with “-lm” on the command line of the same build, as shown below:

~$ 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 we saw before.

Conclusion

In this Linux Hint article about the C language, we explained step by step how to correctly use the fmod() function to get the remainder or modulus after a fractional variable division.

We looked at the syntax, the input and output arguments, and the supported data types for this function.

We also show, with a practical example, the implementation of fmod() and the correct call to the library to which the function belongs at compile time.

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).