Home C Math Functions nearbyint() C nearbyint() Function Definition In C, nearbyint() rounds a double to an integer value using the current rounding mode , and returns that value as a double. Unlike rint() , it does not raise the FE_INEXACT floating-point exception when the value changes.
You pass one double. The result is still a double that holds an integer value. Include <math.h>. On GCC, link with -lm.
Choose nearbyint when you want quiet rounding for display or indexing math and you do not want exception flags to fire for ordinary inexact results.
Syntax Pass the floating-point number. Start with this call.
Call What it does nearbyint(2.5)Rounds with the current mode and returns a double, without raising FE_INEXACT.
Include <math.h>. On GCC, link with -lm. The return type is double, not long.
Library declarations double nearbyint(double x);
float nearbyintf(float x);
long double nearbyintl(long double x); Parameters Parameter Description x Required. The double to round according to the current rounding mode.
Return Value Return Value Description double An integer-valued double produced by the current rounding mode.
C Examples 1. Basic nearbyint 2. Quiet rounding 3. Snap to grid Example 1 — Basic Rounding Round a few values and keep the results as doubles.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.1f\n", nearbyint(3.2));
printf("%.1f\n", nearbyint(3.8));
printf("%.1f\n", nearbyint(-1.2));
return 0;
} How It Works Include math.h. That declares nearbyint.Round down-ish. 3.2 becomes 3.0.Round up-ish. 3.8 becomes 4.0.Still a double. Notice the .0 in the printed form.Example 2 — Quiet Versus rint Idea Use nearbyint when you only need the rounded value, not exception noise.
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 9.75;
double y = nearbyint(x);
printf("rounded = %.2f\n", y);
return 0;
} How It Works Start fractional. 9.75 is not an integer.Call nearbyint. Default mode rounds to the nearest value.No FE_INEXACT. That is the special quiet behavior.Keep as double. Handy before formatting for display.Example 3 — Snap to Whole Numbers Snap sensor readings onto a whole-number grid.
#include <stdio.h>
#include <math.h>
int main(void) {
double reading = 21.6;
double snapped = nearbyint(reading);
printf("snapped = %.0f\n", snapped);
return 0;
} How It Works Read a sample. Real sensors often give fractions.Snap with nearbyint. Map onto the nearest whole value.Print cleanly. Whole numbers look tidy in logs.Mode aware. If you change rounding mode, snapping follows it.Frequently Asked Questions What does nearbyint() do in C? nearbyint() rounds a double using the current rounding mode and returns a double without raising FE_INEXACT.
What is the parameter of nearbyint()? x is required. It is the double to round.
What does nearbyint() return? A double that holds the rounded integer value.
Which header file does nearbyint() need? Include <math.h>. On GCC, link with -lm.
How is nearbyint() different from rint()? Both round the same way for values, but nearbyint() does not raise FE_INEXACT.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion