round, roundf, roundl

Arrondit une valeur à virgule flottante à la valeur entière la plus proche.

Syntaxe

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

Paramètres

x
Valeur à virgule flottante à arrondir.

Valeur retournée

Les fonctions round retournent une valeur à virgule flottante qui représente l'entier le plus proche de x. Les valeurs médianes sont arrondies en s’éloignant de zéro, indépendamment du paramètre du mode d’arrondi à virgule flottante. Il n’y a pas de retour d’erreur.

Entrée Exception SEH Exception _matherr
± QNaN, IND aucune _DOMAIN

Notes

C++ autorisant la surcharge, vous pouvez appeler des surcharges de round qui acceptent et retournent des valeurs float et long double . Dans un programme C, sauf si vous utilisez la <tgmath.h> macro pour appeler cette fonction, round prend toujours et retourne un double.

Si vous utilisez la round macro à partir de <tgmath.h>laquelle , le type de l’argument détermine la version de la fonction sélectionnée. Pour plus d’informations, consultez les mathématiques génériques de type.

Par défaut, l’état global de cette fonction est limité à l’application. Pour modifier ce comportement, consultez État global dans le CRT.

Spécifications

Routine En-tête requis
round, roundf, roundl <math.h>
Macro round <tgmath.h>

Pour plus d’informations sur la compatibilité, consultez Compatibility.

Exemple

// 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

Voir aussi

Prise en charge mathématique et à virgule flottante
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