C remainder() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, remainder() returns the IEEE remainder of x / y. It chooses the remainder so that the quotient is the integer nearest to the exact x / y (ties to even).

You pass two double values. Both are required. The result is a double whose magnitude is at most half of |y|. Include <math.h>. This is different from fmod(), which truncates the quotient toward zero.

When you need the remainder that matches IEEE 754 rules, prefer remainder. When you want the classic “what is left after truncating division,” use fmod.

Syntax

Pass the dividend, then the divisor. Start with this call.

C
remainder(5.1, 3.0);
CallWhat it does
remainder(5.1, 3.0);Returns about -0.9, the IEEE remainder of 5.1 / 3.

Include <math.h>. On GCC, link with -lm. Do not pass 0 for y.

Library declarations
C
double remainder(double x, double y);
float remainderf(float x, float y);
long double remainderl(long double x, long double y);

Parameters

ParameterDescription
xRequired. The dividend.
yRequired. The divisor. It must not be 0.

Return Value

Return ValueDescription
IEEE remainderx - n*y where n is the integer nearest to x/y (ties to even).
NaNReturned for domain errors such as y == 0.

C Examples

Example 1 — Basic IEEE Remainder

Print a simple remainder and notice it can be negative even when both inputs are positive.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%.1f\n", remainder(5.1, 3.0));
    printf("%.1f\n", remainder(9.0, 3.0));
    return 0;
}

How It Works

  1. Include math.h. That declares remainder.
  2. 5.1 / 3. Nearest integer quotient is 2; remainder is about -0.9.
  3. Exact multiple. 9.0 / 3.0 leaves 0.0.
  4. Print as double. %.1f keeps the display simple.

Example 2 — Compare with fmod

Place both results side by side so the rule difference is obvious.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = 5.1;
    double y = 3.0;

    printf("remainder = %.1f\n", remainder(x, y));
    printf("fmod      = %.1f\n", fmod(x, y));
    return 0;
}

How It Works

  1. Same inputs. Use 5.1 and 3.0 for both calls.
  2. IEEE path. remainder picks quotient 2.
  3. Trunc path. fmod truncates toward zero to quotient 1.
  4. Different leftovers. That is why the printed remainders disagree.

Example 3 — Magnitude Stays Within Half of |y|

Confirm that the IEEE remainder never exceeds half the divisor in size.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double y = 4.0;
    double r = remainder(7.0, y);

    printf("r = %.1f\n", r);
    printf("|r| <= |y|/2 ? %s\n", fabs(r) <= fabs(y) / 2.0 ? "yes" : "no");
    return 0;
}

How It Works

  1. Choose y. 4.0 makes half easy to see (2.0).
  2. Compute r. remainder(7, 4) is -1.
  3. Check bound. |r| is 1, which is ≤ 2.
  4. Useful rule. IEEE remainder stays in that half-range.

Frequently Asked Questions

remainder(x, y) returns the IEEE remainder of x divided by y, using the nearest integer quotient.
x is the dividend and y is the divisor. Both are required. y must not be 0.
A double remainder whose magnitude is at most half of |y|, with IEEE rounding of the quotient.
Include <math.h>. On GCC, link with -lm.
fmod uses a quotient truncated toward zero. remainder uses the nearest integer quotient (ties to even), so the sign and value can differ.

Next: remquo()

Get the IEEE-style remainder and a few bits of the quotient.

remquo() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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