C# | Math.IEEERemainder() Method

In C#, IEEERemainder() is a Math class method which is used to return the remainder resulting from the division of a specified number by another specified number.

Syntax:

public static double IEEERemainder (double a, double b);

Parameters:

a: It is the dividend of type System.Double.

b: It is the divisor of type System.Double.



Return Type: This method returns a number equal to a – (b Q), where Q is the quotient of a / b rounded to the nearest integer of type System.Double.

Note:

  • If a / b falls halfway between two integers, the even integer is returned.
  • If a – (b Q) is zero, the value Postive Zero is returned if a is positive, or Negative Zero if a is negative.
  • If b = 0, NaN is returned.

Difference Between IEEERemainder and Remainder Operator: Both are used to returns the remainder after division but the formulas they use are different. The formula for the IEEERemainder method is:

IEEERemainder = dividend - (divisor * Math.Round(dividend / divisor))

And the formula for the remainder operator is:

Remainder = (Math.Abs(dividend) - (Math.Abs(divisor) *  
            (Math.Floor(Math.Abs(dividend) / Math.Abs(divisor))))) *   
            Math.Sign(dividend)

Example:

// C# Program to illlustrate the
// Math.IEEERemainder() Method
using System;
  
class Geeks
{
      
   // method to calucalte the remainder 
   private static void DisplayRemainder(double num1, double num2)
   {
         
      var calculation = $"{num1} / {num2} = ";
        
      // calculating IEEE Remainder
      var ieeerem = Math.IEEERemainder(num1, num2);
        
      // using remainder operator
      var rem_op = num1 % num2;
        
      Console.WriteLine($"{calculation,-16} {ieeerem,18} {rem_op,20}");
   }
     
     
     
   // Main Method
   public static void Main()
   {
         
      Console.WriteLine($"{"IEEERemainder",35} {"Remainder Operator",20}");
        
      // calling the method
      DisplayRemainder(0, 1);
      DisplayRemainder(-4, 8);
      DisplayRemainder(1, 0);
      DisplayRemainder(-1, -0);
      DisplayRemainder(145, 7);
      DisplayRemainder(18.52, 2);
      DisplayRemainder(42.26, 4.2);
   }
}

Output:

                      IEEERemainder   Remainder Operator
0 / 1 =                           0                    0
-4 / 8 =                         -4                   -4
1 / 0 =                         NaN                  NaN
-1 / 0 =                        NaN                  NaN
145 / 7 =                        -2                    5
18.52 / 2 =                    0.52                 0.52
42.26 / 4.2 =     0.259999999999998    0.259999999999996

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.math.ieeeremainder?view=netframework-4.7.2



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.