double trunc(double x); float trunc(float x); long double trunc(long double x); double trunc(T x); //整数类型
trunc()函数采用单个参数,并返回double,float或long double类型的值。此函数在<cmath>头文件中定义。
trunc()函数采用单个参数,其trunc值将被计算。
简而言之,trunc()函数会截断小数点后的值,并且仅返回整数部分。
#include <iostream> #include <cmath> using namespace std; int main() { double x = 10.25, result; result = trunc(x); cout << "trunc(" << x << ") = " << result << endl; x = -34.251; result = trunc(x); cout << "trunc(" << x << ") = " << result << endl; return 0; }
运行该程序时,输出为:
trunc(10.25) = 10 trunc(-34.251) = -34
#include <iostream> #include <cmath> using namespace std; int main() { int x = 15; double result; result = trunc(x); cout << "trunc(" << x << ") = " << result << endl; return 0; }
运行该程序时,输出为:
trunc(15) = 15
对于整数值,应用trunc函数将返回相同的结果。所以它在实际中并不常用来表示整数值。