Home  >  Article  >  Backend Development  >  In C language, trunc() represents a truncation function, truncf() represents a truncation function (single precision), and truncl() represents a truncation function (long double precision).

In C language, trunc() represents a truncation function, truncf() represents a truncation function (single precision), and truncl() represents a truncation function (long double precision).

PHPz
PHPzforward
2023-09-09 10:53:021214browse

In C language, trunc() represents a truncation function, truncf() represents a truncation function (single precision), and truncl() represents a truncation function (long double precision).

Here we will see three functions. These functions are trunc(), truncf(), and truncl(). These functions are used to convert floating point values ​​into truncated form.

trunc() function

This function is used to truncate double type values. And only the integer part is returned. The syntax is as follows.

double trunc(double argument)

Example

#include <stdio.h>
#include <math.h>
main() {
   double a, b, x, y;
   x = 53.26;
   y = 75.86;
   a = trunc(x);
   b = trunc(y);
   printf("The value of a: %lf</p><p>",a);
   printf("The value of a: %lf</p><p>",b);
}

Output

The value of a: 53.000000
The value of a: 75.000000

truncf() function

This function is used to truncate the value of floating point type and return only the integer part. The syntax is as follows. The Chinese translation of

float tuncf(float argument)

Example

is:

Example

#include <stdio.h>
#include <math.h>
main() {
   float a, b, x, y;
   x = 53.26;
   y = 75.86;
   a = truncf(x);
   b = truncf(y);
   printf("The value of a: %f</p><p>",a);
   printf("The value of a: %f</p><p>",b);
}

Output

The value of a: 53.000000
The value of a: 75.000000

truncl() function

This is similar to trunc( ) or truncf(). But the main difference is that this function is used to truncate long double type values. And only the integer part is returned.

The syntax is as follows.

long double truncl(long double argument)

Example

#include <stdio.h>
#include <math.h>
main() {
   long double a, b, x, y;
   x = 53547.55555555555;
   y = 78547.55555555523;
   a = truncl(x);
   b = truncl(y);
   printf("The value of a: %Lf</p><p>",a);
   printf("The value of a: %Lf</p><p>",b);
}

Output

The value of a: 53547.000000
The value of a: 78547.000000

The above is the detailed content of In C language, trunc() represents a truncation function, truncf() represents a truncation function (single precision), and truncl() represents a truncation function (long double precision).. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete