我要评分
获取效率
正确性
完整性
易理解

round

Return the value rounded to the nearest integer for x.

Interface Definition

C interface:

float roundf(float x);

double round(double x);

long double roundl(long double x);

Parameters

Parameter

Type

Description

Input/Output

x

  • For roundf, x is of single-precision floating-point type.
  • For round, x is of double-precision floating-point type.
  • For roundl, x is of long double-precision floating-point type.

Floating-point value of the input data.

Input

Return Value

  • The rounded value of x is returned. x ∈ (-inf, inf)
  • If the input is +0, the return value is +0.
  • If the input is -0, the return value is -0.
  • If the input is ±∞, the return value is ±∞.
  • If the input is NaN, the return value is NaN.

Dependency

C: "km.h"

Example

C interface:
    // typical usage
    double x1 = 0.0, x2 = -0.0, x3 = 1.5, x4 = -2.5;
    // special handing
    double a = INFINITY, b = -INFINITY, c = NAN;
    // print result
    printf("round(0.0) = %.15f\n", round(x1));
    printf("round(-0.0) = %.15f\n", round(x2));
    printf("round(1.5) = %.15f\n", round(x3));
    printf("round(-2.5) = %.15f\n", round(x4));
    printf("round(INFINITY) = %.15f\n", round(a));
    printf("round(-INFINITY) = %.15f\n", round(b));
    printf("round(NAN) = %.15f\n", round(c));
    /* 
     * round(0.0) = 0.000000000000000
     * round(-0.0) = -0.000000000000000
     * round(1.5) = 2.000000000000000
     * round(-2.5) = -3.000000000000000
     * round(INFINITY) = inf
     * round(-INFINITY) = -inf
     * round(NAN) = nan
     * */