nearbyint() Function

The nearbyint() function in C rounds a floating-point number to its nearest integral value, based on the current rounding mode without raising any inexact exceptions. It determines the integral value closest to the given number, making it useful in situations where you need a floating-point result that represents an integer.


Syntax of nearbyint()

</>
Copy
double nearbyint(double x);
float nearbyintf(float x);
long double nearbyintl(long double x);

Parameters

ParameterDescription
xThe value to be rounded to a nearby integral value.

Return Value

The function returns the value of the input rounded to the nearest integral value, still in floating-point format.

The nearbyint() function uses the rounding direction as specified by the current rounding mode (which can be set using functions from fenv.h). Unlike rint(), it does not raise the FE_INEXACT exception when the result is not exact.


Examples for nearbyint()

Example 1: Rounding a Positive Number with Default Rounding Mode

This example demonstrates how to round a positive floating-point number to its nearest integral value using nearbyint().

Program

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

int main() {
    double num = 3.7;
    double rounded = nearbyint(num);
    printf("Rounded value: %.1f\n", rounded);
    return 0;
}

Explanation:

  1. The program includes the necessary headers for input/output and math functions.
  2. A positive floating-point number 3.7 is defined.
  3. The nearbyint() function rounds the number to its nearest integral value.
  4. The rounded value is printed to the console.

Program Output:

Rounded value: 4.0

Example 2: Rounding a Negative Floating-Point Number

This example shows how nearbyint() handles negative numbers by rounding them to the nearest integral value.

Program

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

int main() {
    double num = -2.3;
    double rounded = nearbyint(num);
    printf("Rounded value: %.1f\n", rounded);
    return 0;
}

Explanation:

  1. The program starts by including the required headers.
  2. A negative floating-point number -2.3 is defined.
  3. The nearbyint() function rounds this number to the nearest integral value according to the current rounding mode.
  4. The result is printed, showing how the function deals with negative values.

Program Output:

Rounded value: -2.0

Example 3: Rounding a Fractional Value Close to an Integer Boundary

This example demonstrates the rounding of a fractional number that is very close to an integer boundary.

Program

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

int main() {
    double num = 5.0001;
    double rounded = nearbyint(num);
    printf("Rounded value: %.4f\n", rounded);
    return 0;
}

Explanation:

  1. The necessary headers are included for input/output and mathematical operations.
  2. A fractional value slightly greater than 5 is defined.
  3. The nearbyint() function rounds this value to the nearest integral value.
  4. The result is printed with precision to show the rounded output.

Program Output:

Rounded value: 5.0000