MathF.IEEERemainder() Method in C# with Examples
In C#, IEEERemainder(Single) is a MathF class method which is used to return the remainder resulting from the division of a specified number by another specified number.
Syntax: public static float IEEERemainder (float x, float y);
Parameters:
x: It is the dividend of type System.Single.
y: It is the divisor of type System.Single.
Return Type: This method returns a number equal to x – (y Q), where Q is the quotient of x / y rounded to the nearest integer of type System.Single.
Note:
- If x / y falls halfway between two integers, the even integer is returned.
- If x – (y Q) is zero, the value Positive Zero is returned if x is positive, or Negative Zero if y is negative.
- If y = 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 * MathF.Round(dividend / divisor))
And the formula for the remainder operator is:
Remainder = (MathF.Abs(dividend) - (MathF.Abs(divisor) * (MathF.Floor(MathF.Abs(dividend) / MathF.Abs(divisor))))) * MathF.Sign(dividend)
Example:
CSharp
// C# Program to illustrate the use of // MathF.IEEERemainder(Single, Single) // Method using System; class Beginner { // Method to calculate the remainder private static void DisplayRemainder( float x, float y) { var calculation = $ "{x} / {y} = " ; // calculating IEEE Remainder var ieeerem = MathF.IEEERemainder(x, y); // using remainder operator var rem_op = x % y; 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(0f, 1f); DisplayRemainder(-4f, 8f); DisplayRemainder(1f, 0f); DisplayRemainder(-1f, -0f); DisplayRemainder(175f, 6f); DisplayRemainder(784.52f, 124f); DisplayRemainder(92.267f, 3.259f); } } |
Output:
IEEERemainder Remainder Operator 0 / 1 = 0 0 -4 / 8 = -4 -4 1 / 0 = NaN NaN -1 / 0 = NaN NaN 175 / 6 = 1 1 784.52 / 124 = 40.52002 40.52002 92.267 / 3.259 = 1.014997 1.014997