Mathwords logoMathwords

Nearest Integer Function — Definition, Formula & Examples

The nearest integer function rounds a real number to the closest integer. When the fractional part is exactly 0.5, the convention typically rounds to the nearest even integer (called banker's rounding), though some textbooks round up.

For a real number xx, the nearest integer function nint(x)\text{nint}(x) returns the integer nn that minimizes xn|x - n|. If xx lies exactly halfway between two consecutive integers (i.e., the fractional part equals 0.50.5), the tie-breaking rule varies by convention — commonly rounding to the nearest even integer or rounding away from zero.

Key Formula

nint(x)=x+0.5\text{nint}(x) = \left\lfloor x + 0.5 \right\rfloor
Where:
  • xx = Any real number to be rounded
  • \lfloor \cdot \rfloor = The floor function (greatest integer less than or equal to the argument)

How It Works

To evaluate nint(x)\text{nint}(x), look at the fractional part of xx. If it is less than 0.50.5, round down to the integer below. If it is greater than 0.50.5, round up to the integer above. If the fractional part is exactly 0.50.5, apply whichever tie-breaking rule your course specifies. The graph of this function is a step function with jumps occurring at each half-integer value (e.g., 0.5,1.5,2.5,0.5, 1.5, 2.5, \ldots).

Worked Example

Problem: Find nint(3.7), nint(−2.3), and nint(4.5).
Step 1: For 3.7, the fractional part is 0.7, which is greater than 0.5, so round up.
nint(3.7)=4\text{nint}(3.7) = 4
Step 2: For −2.3, the fractional distance to −2 is 0.3 and to −3 is 0.7, so the nearest integer is −2.
nint(2.3)=2\text{nint}(-2.3) = -2
Step 3: For 4.5, the value is exactly halfway between 4 and 5. Using the round-half-up convention: round up to 5. Using round-half-to-even: the nearest even integer is 4.
nint(4.5)=5  (round-half-up)or4  (round-half-to-even)\text{nint}(4.5) = 5 \;\text{(round-half-up)} \quad \text{or} \quad 4 \;\text{(round-half-to-even)}
Answer: nint(3.7) = 4, nint(−2.3) = −2, and nint(4.5) = 5 or 4 depending on the tie-breaking convention.

Why It Matters

Rounding to the nearest integer shows up in computer science (pixel coordinates, signal processing) and statistics (reporting whole-number counts). In precalculus, it provides practice with piecewise and step functions and helps you understand discontinuities at half-integer points.

Common Mistakes

Mistake: Confusing the nearest integer function with the floor (greatest integer) function.
Correction: The floor function always rounds down: 2.7=2\lfloor 2.7 \rfloor = 2. The nearest integer function rounds to whichever integer is closer: nint(2.7)=3\text{nint}(2.7) = 3. They differ whenever the fractional part exceeds 0.5.