Home > Backend Development > C++ > 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
Release: 2023-09-09 10:53:02
forward
1356 people have browsed it

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)
Copy after login

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);
}
Copy after login

Output

The value of a: 53.000000
The value of a: 75.000000
Copy after login
Copy after login

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)
Copy after login

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);
}
Copy after login

Output

The value of a: 53.000000
The value of a: 75.000000
Copy after login
Copy after login

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)
Copy after login

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);
}
Copy after login

Output

The value of a: 53547.000000
The value of a: 78547.000000
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template