Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
Title: '.IEEERemainder()'
Description: 'Returns the remainder resulting from the division of two specified numbers according to the IEEE 754 standard.'
Subjects:
- 'Computer Science'
- 'Code Foundations'
Tags:
- 'Methods'
- 'Numbers'
- 'Arithmetic'
- 'Functions'
CatalogContent:
- 'learn-c-sharp'
- 'paths/computer-science'
---

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.

## Syntax

```pseudo
Math.IEEERemainder(x, y)
```

**Parameters:**

- `x`: A `double` representing the dividend (the number to be divided).
- `y`: A `double` representing the divisor (the number to divide by).

**Return value:**

Returns a `double` representing the remainder of `x` divided by `y` according to IEEE 754. Special cases include:

- If `x` is `NaN`, returns `NaN`.
- If `y` is `NaN`, returns `NaN`.
- If `x` is positive or negative infinity, returns `NaN`.
- If `y` is zero, returns `NaN`.

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.

## Example

The following example demonstrates the `Math.IEEERemainder()` method and compares it with the modulo operator:

```cs
using System;

public class Example
{
public static void Main()
{
double dividend = 17.8;
double divisor = 4.0;

double ieeeRemainder = Math.IEEERemainder(dividend, divisor);
double moduloRemainder = dividend % divisor;

Console.WriteLine($"Dividend: {dividend}");
Console.WriteLine($"Divisor: {divisor}");
Console.WriteLine($"IEEE Remainder: {ieeeRemainder}");
Console.WriteLine($"Modulo Remainder: {moduloRemainder}");
}
}
```

This example results in the following output:

```shell
Dividend: 17.8
Divisor: 4.0
IEEE Remainder: 1.8000000000000007
Modulo Remainder: 1.8
```

> **Note:** The slight difference in the IEEE Remainder output (1.8000000000000007) is due to floating-point precision limitations in binary representation.

## Codebyte Example

The following example is runnable and demonstrates how `Math.IEEERemainder()` handles different values:

```codebyte/csharp
using System;

public class Example
{
public static void Main()
{
// Example 1: Positive numbers
Console.WriteLine("Example 1: Positive numbers");
Console.WriteLine($"IEEERemainder(10, 3) = {Math.IEEERemainder(10, 3)}");
Console.WriteLine($"Modulo (10 % 3) = {10 % 3}");
Console.WriteLine();

// Example 2: Negative dividend
Console.WriteLine("Example 2: Negative dividend");
Console.WriteLine($"IEEERemainder(-10, 3) = {Math.IEEERemainder(-10, 3)}");
Console.WriteLine($"Modulo (-10 % 3) = {-10 % 3}");
Console.WriteLine();

// Example 3: Demonstrating the key difference
Console.WriteLine("Example 3: Showing IEEE remainder behavior");
Console.WriteLine($"IEEERemainder(5, 3) = {Math.IEEERemainder(5, 3)}");
Console.WriteLine($"Modulo (5 % 3) = {5 % 3}");
}
}
```
110 changes: 110 additions & 0 deletions content/cpp/concepts/unordered-set/terms/empty/empty.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
Title: 'empty()'
Description: 'Checks whether the unordered set is empty or not.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Sets'
- 'STL'
- 'Unordered Sets'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

The **`empty()`** method checks whether an [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set) is empty (contains no elements). It returns `true` if the container has no elements, and `false` if it contains at least one element.

## Syntax

```pseudo
unordered_set_name.empty();
```

**Return value:**

Returns a boolean value:

- `true` if the `unordered_set` is empty (size is 0)
- `false` if the `unordered_set` contains one or more elements

## Example

This example demonstrates checking if an `unordered_set` is empty before and after adding elements:

```cpp
#include <iostream>
#include <unordered_set>
using namespace std;

int main() {
unordered_set<int> numbers;

// Check if the set is initially empty
if (numbers.empty()) {
cout << "The set is empty.\n";
}

// Add elements to the set
numbers.insert(10);
numbers.insert(20);
numbers.insert(30);

// Check again after adding elements
if (!numbers.empty()) {
cout << "The set is not empty. It contains " << numbers.size() << " elements.\n";
}

// Clear the set
numbers.clear();

// Check if empty after clearing
if (numbers.empty()) {
cout << "The set is empty again after clearing.\n";
}

return 0;
}
```

The output of this code is:

```shell
The set is empty.
The set is not empty. It contains 3 elements.
The set is empty again after clearing.
```

## Codebyte Example

This example shows how `empty()` can be used to process all elements in an `unordered_set` safely:

```codebyte/cpp
#include <iostream>
#include <unordered_set>
#include <string>
using namespace std;

int main() {
unordered_set<string> fruits = {"apple", "banana", "cherry"};

cout << "Processing fruits:\n";

// Process elements until the set is empty
while (!fruits.empty()) {
// Get an element (we can't predict which one due to unordered nature)
auto it = fruits.begin();
cout << "Processing: " << *it << "\n";

// Remove the processed element
fruits.erase(it);
}

// Verify the set is empty
if (fruits.empty()) {
cout << "All fruits have been processed!\n";
}

return 0;
}
```