C# Tutorial C# Advanced C# References

C# Math - IEEERemainder() Method



The C# IEEERemainder() method is used to compute the remainder operation on two arguments. The remainder can be expressed as follows:

Remainder = x - y * Q, where Q is the quotient of x/y rounded to the nearest integer. If x/y falls exactly at the midway between two integers, the even integer is returned.

In special cases it returns the following:

  • If y is zero, the method returns NaN.
  • If x - y * Q is zero, the method returns +0 if x is positive and -0 if x is negative.

Syntax

public static double IEEERemainder (double x, double y);

Parameters

x Specify the dividend.
y Specify the divisor.

Return Value

Returns the remainder when x is divided by y.

Example:

In the example below, IEEERemainder() method is used to calculate the remainder operation on two given arguments.

using System;

class MyProgram {
  static void Main(string[] args) {
    Console.WriteLine("Math.IEEERemainder(10, 4) = "
                      + Math.IEEERemainder(10, 4));
    Console.WriteLine("Math.IEEERemainder(14, 4) = "
                      + Math.IEEERemainder(14, 4));
    Console.WriteLine("Math.IEEERemainder(60.8, 18.1) = "
                      + Math.IEEERemainder(60.8, 18.1));
    Console.WriteLine("Math.IEEERemainder(Double.NaN, 4) = "
                      + Math.IEEERemainder(Double.NaN, 4));
    Console.WriteLine("Math.IEEERemainder(Double.NegativeInfinity, 4) = "
                      + Math.IEEERemainder(Double.NegativeInfinity, 4));
    Console.WriteLine("Math.IEEERemainder(Double.PositiveInfinity, 4) = "
                      + Math.IEEERemainder(Double.PositiveInfinity, 4));  
  }
}

The output of the above code will be:

Math.IEEERemainder(10, 4) = 2
Math.IEEERemainder(14, 4) = -2
Math.IEEERemainder(60.8, 18.1) = 6.49999999999999
Math.IEEERemainder(Double.NaN, 4) = NaN
Math.IEEERemainder(Double.NegativeInfinity, 4) = NaN
Math.IEEERemainder(Double.PositiveInfinity, 4) = NaN

❮ C# Math Methods