round函式是C++中的Round()的含義。
基本介紹
- 中文名:round函式
- 格式:double round (double);
- 舉例:round(1.49);//Returns 1.0
- 含義:C++中的Round()
格式
double round (double x);float round (float x);long double round (long double x);double round (T x);
舉例
/* round vs floor vs ceil vs trunc */#include <stdio.h> /* printf */#include <math.h> /* round, floor, ceil, trunc */int main (){ const char * format = "%.1f \t%.1f \t%.1f \t%.1f \t%.1f\n"; printf ("value\tround\tfloor\tceil\ttrunc\n"); printf ("-----\t-----\t-----\t----\t-----\n"); printf (format, 2.3,round( 2.3),floor( 2.3),ceil( 2.3),trunc( 2.3)); printf (format, 3.8,round( 3.8),floor( 3.8),ceil( 3.8),trunc( 3.8)); printf (format, 5.5,round( 5.5),floor( 5.5),ceil( 5.5),trunc( 5.5)); printf (format,-2.3,round(-2.3),floor(-2.3),ceil(-2.3),trunc(-2.3)); printf (format,-3.8,round(-3.8),floor(-3.8),ceil(-3.8),trunc(-3.8)); printf (format,-5.5,round(-5.5),floor(-5.5),ceil(-5.5),trunc(-5.5)); return 0;}輸出:value round floor ceil trunc----- ----- ----- ---- -----2.3 2.0 2.0 3.0 2.03.8 4.0 3.0 4.0 3.05.5 6.0 5.0 6.0 5.0-2.3 -2.0 -3.0 -2.0 -2.0-3.8 -4.0 -4.0 -3.0 -3.0-5.5 -6.0 -6.0 -5.0 -5.0
源碼
/* 7.12.9.6 *//* round away from zero, regardless of fpu control word settings */extern double __cdecl round (double);extern float __cdecl roundf (float);extern long double __cdecl roundl (long double); /* 7.12.9.7 */extern long __cdecl lround (double);extern long __cdecl lroundf (float);extern long __cdecl lroundl (long double); extern long long __cdecl llround (double);extern long long __cdecl llroundf (float);extern long long __cdecl llroundl (long double);
#undef llround#undef llroundf#undef llroundl#undef lround#undef lroundf#undef lroundl#undef round#undef roundf#undef roundl using ::llround; using ::llroundf; using ::llroundl; using ::lround; using ::lroundf; using ::lroundl; using ::round; using ::roundf; using ::roundl; constexpr long long llround(float __x) { return __builtin_llroundf(__x); } constexpr long long llround(long double __x) { return __builtin_llroundl(__x); } template<typename _Tp> constexpr typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, long long>::__type llround(_Tp __x) { return __builtin_llround(__x); } constexpr long lround(float __x) { return __builtin_lroundf(__x); } constexpr long lround(long double __x) { return __builtin_lroundl(__x); } template<typename _Tp> constexpr typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, long>::__type lround(_Tp __x) { return __builtin_lround(__x); } constexpr float round(float __x) { return __builtin_roundf(__x); } constexpr long double round(long double __x) { return __builtin_roundl(__x); } template<typename _Tp> constexpr typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, double>::__type round(_Tp __x) { return __builtin_round(__x); }