Skip to content

Commit 30a300c

Browse files
committed
Add C# Math.IEEEremainder() documentation entry
1 parent 95f0e9a commit 30a300c

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
Title: '.IEEERemainder()'
3+
Description: 'Returns the remainder of dividing two specified numbers as defined by the IEEE 754 standard.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Arithmetic'
9+
- 'Functions'
10+
- 'Methods'
11+
- 'Numbers'
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 one specified number by another, as defined by the IEEE 754 standard. This differs from the modulo operator (`%`) in how it handles the remainder calculation.
18+
19+
## Syntax
20+
21+
```pseudo
22+
Math.IEEERemainder(x, y);
23+
```
24+
25+
**Parameters:**
26+
27+
- `x`: The dividend (type `double`).
28+
- `y`: The divisor (type `double`).
29+
30+
**Return value:**
31+
32+
Returns a `double` value equal to `x - (y * n)`, where `n` is the closest integer to `x / y`. If `x / y` is exactly halfway between two integers, `n` is chosen to be the even integer. Special cases include:
33+
34+
- If the result is zero, it has the same sign as `x`
35+
- If `y` is zero, returns `NaN`
36+
- If `x` is infinity, returns `NaN`
37+
- If `y` is infinity, returns `x`
38+
39+
> **Note:** The IEEE remainder differs from the standard modulo operation. For example, `5 % 3` equals `2` in C#, but `Math.IEEERemainder(5, 3)` equals `-1` because `5 / 3` rounds to `2` (nearest integer), and `5 - (3 * 2) = -1`.
40+
41+
## Example
42+
43+
The following example demonstrates using the `Math.IEEERemainder()` method and shows how it differs from the modulo operator:
44+
45+
```cs
46+
using System;
47+
48+
public class IEEERemainderExample
49+
{
50+
public static void Main()
51+
{
52+
double dividend = 5.0;
53+
double divisor = 3.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+
The example above produces the following output:
67+
68+
```shell
69+
Dividend: 5
70+
Divisor: 3
71+
IEEE Remainder: -1
72+
Modulo Remainder: 2
73+
```
74+
75+
## Codebyte Example
76+
77+
The following runnable example demonstrates how `Math.IEEERemainder()` behaves differently from the modulo operator in various scenarios:
78+
79+
```codebyte/csharp
80+
using System;
81+
82+
public class Program
83+
{
84+
public static void Main()
85+
{
86+
// Compare IEEE remainder with modulo operator
87+
double[] dividends = { 5.0, 7.0, 14.0, -5.0 };
88+
double[] divisors = { 3.0, 4.0, 4.0, 3.0 };
89+
90+
Console.WriteLine("Dividend\tDivisor\t\tIEEE Rem\tModulo");
91+
Console.WriteLine("--------------------------------------------------------");
92+
93+
for (int i = 0; i < dividends.Length; i++)
94+
{
95+
double ieee = Math.IEEERemainder(dividends[i], divisors[i]);
96+
double modulo = dividends[i] % divisors[i];
97+
Console.WriteLine($"{dividends[i]}\t\t{divisors[i]}\t\t{ieee}\t\t{modulo}");
98+
}
99+
}
100+
}
101+
```

0 commit comments

Comments
 (0)