Udostępnij za pośrednictwem


round, roundf, roundl

Zaokrągla wartość zmiennoprzecinkową do najbliższej wartości całkowitej.

Składnia

double round(
   double x
);
float round(
   float x
);  // C++ only
long double round(
   long double x
);  // C++ only
float roundf(
   float x
);
long double roundl(
   long double x
);
#define round(X) // Requires C11 or higher

Parametry

x
Wartość zmiennoprzecinkowa do zaokrąglonej.

Wartość zwracana

Funkcje round zwracają wartość zmiennoprzecinkową, która reprezentuje najbliższą liczbę całkowitą do x. Wartości w połowie są zaokrąglane od zera, niezależnie od ustawienia trybu zaokrąglania zmiennoprzecinkowego. Nie ma zwracanego błędu.

Dane wejściowe Wyjątek SEH Wyjątek _matherr
± QNaN, IND Brak _DOMAIN

Uwagi

Ponieważ język C++ umożliwia przeciążenie, można wywołać przeciążenia tych wartości round , które przyjmują i zwracają float i long double wartości. W programie języka C, chyba że używasz makra <tgmath.h> do wywoływania tej funkcji, round zawsze przyjmuje i zwraca wartość double.

Jeśli używasz makra round z <tgmath.h>, typ argumentu określa, która wersja funkcji jest zaznaczona. Aby uzyskać szczegółowe informacje, zobacz Typy ogólne matematyczne .

Domyślnie stan globalny tej funkcji jest zakresem aplikacji. Aby zmienić to zachowanie, zobacz Stan globalny w CRT.

Wymagania

Procedura Wymagany nagłówek
round, roundf, roundl <math.h>
round Makro <tgmath.h>

Aby uzyskać więcej informacji o zgodności, zobacz Zgodność.

Przykład

// Build with: cl /W3 /Tc
// This example displays the rounded
// results of floating-point values

#include <math.h>
#include <stdio.h>

int main()
{
    printf("===== Round a float\n\n");
    float floatValue = 2.4999999f; // float stores a value close to, but not exactly equal to, the initializer below. floatValue will contain 2.5 because it is the closest single precision value
    printf("roundf(%.1000g) is %.1000g\n", floatValue, roundf(floatValue));
    printf("roundf(%.1000g) is %.1000g\n", -floatValue, roundf(-floatValue));

    // double stores a value close to, but not exactly equal to, the initializer below. The closest double value is just slightly larger.
    double doubleValue = 2.4999999;
    printf("\n===== Round a double\n\n");
    printf("round(%.1000g) is %.1000g\n", doubleValue, round(doubleValue));
    printf("round(%.1000g) is %.1000g\n", -doubleValue, round(-doubleValue));

    // long double stores a value close to, but not exactly equal to, the initializer below. The closest long double value is just slightly larger.
    long double longDoubleValue = 2.4999999L;
    printf("\n===== Round a long double\n\n");
    printf("roundl(%.1000g) is %.1000g\n", longDoubleValue, roundl(longDoubleValue));
    printf("roundl(%.1000g) is %.1000g\n", -longDoubleValue, roundl(-longDoubleValue));

    return 0;
}
===== Round a float

roundf(2.5) is 3
roundf(-2.5) is -3

===== Round a double

round(2.499999900000000163657887242152355611324310302734375) is 2
round(-2.499999900000000163657887242152355611324310302734375) is -2

===== Round a long double

roundl(2.499999900000000163657887242152355611324310302734375) is 2
roundl(-2.499999900000000163657887242152355611324310302734375) is -2

Zobacz też

Obsługa obliczeń matematycznych i zmiennoprzecinkowych
ceil, ceilf, ceill
floor, floorf, floorl
fmod, fmodf
lrint, lrintf, lrintl, llrint, llrintf, llrintl
lround, lroundf, lroundl, llround, llroundf, llroundl
nearbyint, nearbyintf, nearbyintl
rint, rintf, rintl