C# Math IEEERemainder() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In C#, Math.IEEERemainder(x, y) returns what is left after dividing x by y, but with a twist: the quotient is rounded to the nearest whole number, not cut off. So the answer is the smallest possible leftover, which may be negative. Math.IEEERemainder(11, 3) is -1, because 11 is 1 less than 12, the nearest multiple of 3.

Think of a clock. If it is 11 and you ask "how far from the nearest multiple of 3?", the nearest is 12, one step away, so you are at -1. The ordinary % operator always cuts the quotient down and says 11 % 3 = 2. Both are "correct", but they answer different questions. IEEERemainder is handy when you want the shortest signed distance, such as turning an angle into the range -180 to 180. It is an advanced tool defined by the IEEE 754 standard.

This is a static member of the static class System.Math. You call it with the class name, such as Math.IEEERemainder(...). There is no Math object to create. All the work is done by the method you call, and it never changes your variables. IEEERemainder is in every .NET version. It takes and returns double.

Syntax

Pass the dividend first, then the divisor. Both are double values and the answer is a double.

C#
double a = Math.IEEERemainder(10, 3);   // 1   (10 - 3 * 3)
double b = Math.IEEERemainder(11, 3);   // -1  (11 - 3 * 4)
double c = 11 % 3;                      // 2   (the % operator)
CallWhat it does
Math.IEEERemainder(10, 3)10 / 3 is about 3.33, the nearest whole number is 3, so 10 - 9 = 1.
Math.IEEERemainder(11, 3)11 / 3 is about 3.67, the nearest whole number is 4, so 11 - 12 = -1.
Math.IEEERemainder(7, 2)7 / 2 is exactly 3.5, a tie. Ties go to the even number 4, so 7 - 8 = -1.
Math.IEEERemainder(5, 0)Returns NaN. You cannot divide by zero.

The formula is x - y * Round(x / y), where Round goes to the nearest whole number and ties go to the even number. The result has a size of at most half of |y|. A zero divisor, an infinite x, or any NaN gives NaN. If y is infinite, you simply get x back. No exception is thrown.

Library declarations
C#
public static double IEEERemainder(double x, double y);

Parameters

ParameterDescription
xRequired. The dividend: the number to divide.
yRequired. The divisor: the number to divide by.

Return Value

Return ValueDescription
doubleThe IEEE remainder x - y * n, where n is the whole number nearest to x / y. Its size is never more than half of |y|. NaN if y is 0 or x is infinite.

C# Examples

Example 1 — Remainders That Can Be Negative

Call IEEERemainder on several numbers and see when the answer is positive, negative, or zero.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Math.IEEERemainder(10, 3));
        Console.WriteLine(Math.IEEERemainder(11, 3));
        Console.WriteLine(Math.IEEERemainder(12, 3));
        Console.WriteLine(Math.IEEERemainder(7, 2));
        Console.WriteLine(Math.IEEERemainder(5, 2));
        Console.WriteLine(Math.IEEERemainder(17, 5));
    }
}

How It Works

  1. 10 and 11 by 3. 10 / 3 is about 3.33, which rounds to 3, so the remainder is 10 - 9 = 1. 11 / 3 is about 3.67, which rounds to 4, so the remainder is 11 - 12 = -1.
  2. Exact. 12 is a multiple of 3, so nothing is left over: 0.
  3. Ties. 7 / 2 is exactly 3.5. Ties go to the even whole number, 4, giving 7 - 8 = -1. For 5 / 2 = 2.5 the even number is 2, giving 5 - 4 = 1.
  4. Small leftovers. 17 / 5 is 3.4, which rounds to 3, so the remainder is 17 - 15 = 2, a positive answer, since it is less than half of 5.

Example 2 — IEEERemainder Versus the % Operator

Print the % result and the IEEERemainder result side by side for the numbers 7 to 11 divided by 3.

C#
using System;

class Program {
    static void Main() {
        for (int n = 7; n <= 11; n++) {
            Console.WriteLine($"{n}: {n % 3} {Math.IEEERemainder(n, 3)}");
        }
    }
}

How It Works

  1. Two columns. After the number, the first value is n % 3 and the second is Math.IEEERemainder(n, 3).
  2. % only goes up. The % column cycles 1, 2, 0, 1, 2. It cuts the quotient down, so the leftover is never negative here.
  3. IEEERemainder stays close. Its column runs 1, -1, 0, 1, -1. The leftover is always within half of 3, so numbers that are close to the next multiple get a negative answer.
  4. Same when small. For 7, 9, and 10, the two answers agree (1, 0, 1). They differ for 8 and 11, where the leftover of 2 is more than half of 3.

Example 3 — Wrap an Angle into -180 to 180

Use IEEERemainder with 360 to turn any angle into the shortest turn, then see what a zero divisor gives.

C#
using System;

class Program {
    static void Main() {
        double[] angles = { 270, 190, 30, -200 };

        foreach (double angle in angles) {
            Console.WriteLine(Math.IEEERemainder(angle, 360));
        }

        Console.WriteLine(Math.IEEERemainder(5, 0));
    }
}

How It Works

  1. The idea. A turn of 270 degrees is the same as a turn of -90 degrees. The shortest turn is what you want, and 360 is one full circle.
  2. 270 and 190. 270 / 360 rounds to 1, so 270 - 360 = -90. 190 / 360 rounds to 1, so 190 - 360 = -170.
  3. 30 and -200. 30 is already small, so it stays 30. -200 / 360 rounds to -1, so -200 + 360 = 160.
  4. Zero divisor. IEEERemainder(5, 0) has no answer and returns NaN. Unlike integer %, it does not throw an exception.

Frequently Asked Questions

It returns x - y * n, where n is the whole number nearest to x / y (ties go to the even number). The result is the smallest possible leftover and may be negative. Math.IEEERemainder(11, 3) is -1.
% cuts the quotient toward zero, so 11 % 3 is 2. IEEERemainder rounds the quotient to the nearest whole number, so Math.IEEERemainder(11, 3) is -1. The results match only when the leftover is less than half of the divisor.
Use it when you want the shortest signed distance to a multiple, such as wrapping an angle into -180 to 180 with Math.IEEERemainder(angle, 360). For simple "which slot" maths, use %.
It returns NaN. An infinite x also gives NaN, and an infinite y gives back x. No exception is thrown.
No. Math is a static class, so you call Math.IEEERemainder(...) straight on the class name. You cannot write new Math(), and there is no variable to keep.

Next: ILogB()

Return the binary exponent of a number as an int: the power of two just below it.

ILogB() tutorial →

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.

10 people found this page helpful