C Library - <tgmath.h>



The C library <tgmath.h> contains the headers <math.h> and <complex.h> and defines various types of macros. This function is applicable when real or complex function called on the basis of parameters(types).

It allows computer programmer to express the mathematical operation which works with any floating type such as float, long, double and long double. We can use the single function name with regards to type and make the code easier to read.

Syntax

Following is the syntax of the C library header <tgmath.h> provides various built-in functions−

pow(var1, var2)
Or,
fmod(var1, var2)
Or,
sin(var1, var2) etc.

Parameter

The above syntaxes uses various fuction which accepts only two parameters to express the mathematical expression. For example- finding the power of two numbers, getting trigonometric value, etc. The var1 and var2 belongs to same or different data types.

Return Type

The function returns based on the type of parameters passed to the macros.

Ensure that, <tgmath.h> simplify the code maintenance and uses mathematical operation with the help of type-generic macros.

Example 1

The C library header <tgmath.h> illustrate the function fmod() which accepts two parameters of different datatypes and calculates the remainders of the division of two numbers.

#include <tgmath.h>
#include <stdio.h>

int main() {
   float f = 3.0f;
   double d = 3.0;

   // result is of type double
   double result = fmod(f, d); 

   printf("The result of fmod(%f, %f) = %f\n", f, d, result);
   return 0;
}

The above code produces the following result −

The result of fmod(3.000000, 3.000000) = 0.000000

Example 2

Following example illustrates the usage of cabs() function that determine the abosolute value of complex number and display the result.

#include <tgmath.h>
#include <complex.h>
#include <stdio.h>

int main() {
   float complex fc = 3.0f + 4.0f * I;
   double complex dc = 3.0 + 4.0 * I;
   long double complex ldc = 3.0l + 4.0l * I;

   // res_fc is of type float
   float res_fc = cabs(fc); 
    
   // res_dc is of type double
   double res_dc = cabs(dc);  
    
   // res_ldc is of type long double
   long double res_ldc = cabs(ldc); 

   printf("cabs(%f + %fi) = %f\n", crealf(fc), cimagf(fc), res_fc);
   printf("cabs(%f + %fi) = %f\n", creal(dc), cimag(dc), res_dc);
   printf("cabs(%Lf + %Lfi) = %Lf\n", creall(ldc), cimagl(ldc), res_ldc);

   return 0;
}

On execution of above code, we get the following result −

cabs(3.000000 + 4.000000i) = 5.000000
cabs(3.000000 + 4.000000i) = 5.000000
cabs(3.000000 + 4.000000i) = 5.000000

Example 3

Here, we use the trigonometric function to represent the value of data types − float, double, and long double.

#include <tgmath.h>
#include <stdio.h>
int main() {
   float f = 3.0f;
   double d = 3.0;
   long double ld = 3.0l;

   // result_f is of type float
   double result_f = sin(f);   
   
   // result_d is of type double
   double result_d = sin(d);  
   
   // result_ld is of type long double
   double result_ld = sin(ld); 

   printf("The value of float [sin(%f)] = %f\n", f, result_f);
   printf("The value of double [sin(%f)] = %f\n", d, result_d);
   printf("The value of long [double sin(%Lf)] = %f\n", ld, result_ld);

   return 0;
}

After executing the above code, we get the following result −

The value of float [sin(3.000000)] = 0.141120
The value of double [sin(3.000000)] = 0.141120
The value of long [double sin(3.000000)] = 0.141120
Advertisements