|
| 1 | +--- |
| 2 | +Title: '.IEEERemainder()' |
| 3 | +Description: 'Returns the remainder resulting from the division of two specified numbers according to the IEEE 754 standard.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Code Foundations' |
| 7 | +Tags: |
| 8 | + - 'Methods' |
| 9 | + - 'Numbers' |
| 10 | + - 'Arithmetic' |
| 11 | + - 'Functions' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-sharp' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`Math.IEEERemainder()`** method returns the remainder resulting from the division of two specified numbers according to the IEEE 754 standard. This differs from the standard modulo (%) operator in how it calculates the remainder. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +Math.IEEERemainder(x, y) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `x`: A `double` representing the dividend (the number to be divided). |
| 28 | +- `y`: A `double` representing the divisor (the number to divide by). |
| 29 | + |
| 30 | +**Return value:** |
| 31 | + |
| 32 | +Returns a `double` representing the remainder of `x` divided by `y` according to IEEE 754. Special cases include: |
| 33 | + |
| 34 | +- If `x` is `NaN`, returns `NaN`. |
| 35 | +- If `y` is `NaN`, returns `NaN`. |
| 36 | +- If `x` is positive or negative infinity, returns `NaN`. |
| 37 | +- If `y` is zero, returns `NaN`. |
| 38 | + |
| 39 | +The IEEE remainder differs from the modulo operator (%) in that it uses the rounding mode of "round to nearest" when computing the quotient. The result can be negative even if both operands are positive. |
| 40 | + |
| 41 | +## Example |
| 42 | + |
| 43 | +The following example demonstrates the `Math.IEEERemainder()` method and compares it with the modulo operator: |
| 44 | + |
| 45 | +```cs |
| 46 | +using System; |
| 47 | + |
| 48 | +public class Example |
| 49 | +{ |
| 50 | + public static void Main() |
| 51 | + { |
| 52 | + double dividend = 17.8; |
| 53 | + double divisor = 4.0; |
| 54 | + |
| 55 | + double ieeeRemainder = Math.IEEERemainder(dividend, divisor); |
| 56 | + double moduloRemainder = dividend % divisor; |
| 57 | + |
| 58 | + Console.WriteLine($"Dividend: {dividend}"); |
| 59 | + Console.WriteLine($"Divisor: {divisor}"); |
| 60 | + Console.WriteLine($"IEEE Remainder: {ieeeRemainder}"); |
| 61 | + Console.WriteLine($"Modulo Remainder: {moduloRemainder}"); |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +This example results in the following output: |
| 67 | + |
| 68 | +```shell |
| 69 | +Dividend: 17.8 |
| 70 | +Divisor: 4.0 |
| 71 | +IEEE Remainder: 1.8000000000000007 |
| 72 | +Modulo Remainder: 1.8 |
| 73 | +``` |
| 74 | + |
| 75 | +> **Note:** The slight difference in the IEEE Remainder output (1.8000000000000007) is due to floating-point precision limitations in binary representation. |
| 76 | +
|
| 77 | +## Codebyte Example |
| 78 | + |
| 79 | +The following example is runnable and demonstrates how `Math.IEEERemainder()` handles different values: |
| 80 | + |
| 81 | +```codebyte/csharp |
| 82 | +using System; |
| 83 | +
|
| 84 | +public class Example |
| 85 | +{ |
| 86 | + public static void Main() |
| 87 | + { |
| 88 | + // Example 1: Positive numbers |
| 89 | + Console.WriteLine("Example 1: Positive numbers"); |
| 90 | + Console.WriteLine($"IEEERemainder(10, 3) = {Math.IEEERemainder(10, 3)}"); |
| 91 | + Console.WriteLine($"Modulo (10 % 3) = {10 % 3}"); |
| 92 | + Console.WriteLine(); |
| 93 | +
|
| 94 | + // Example 2: Negative dividend |
| 95 | + Console.WriteLine("Example 2: Negative dividend"); |
| 96 | + Console.WriteLine($"IEEERemainder(-10, 3) = {Math.IEEERemainder(-10, 3)}"); |
| 97 | + Console.WriteLine($"Modulo (-10 % 3) = {-10 % 3}"); |
| 98 | + Console.WriteLine(); |
| 99 | +
|
| 100 | + // Example 3: Demonstrating the key difference |
| 101 | + Console.WriteLine("Example 3: Showing IEEE remainder behavior"); |
| 102 | + Console.WriteLine($"IEEERemainder(5, 3) = {Math.IEEERemainder(5, 3)}"); |
| 103 | + Console.WriteLine($"Modulo (5 % 3) = {5 % 3}"); |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
0 commit comments