diff --git a/content/c-sharp/concepts/math-functions/terms/ieeeremainder/ieeeremainder.md b/content/c-sharp/concepts/math-functions/terms/ieeeremainder/ieeeremainder.md new file mode 100644 index 00000000000..2a71e8c551f --- /dev/null +++ b/content/c-sharp/concepts/math-functions/terms/ieeeremainder/ieeeremainder.md @@ -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}"); + } +} +``` diff --git a/content/cpp/concepts/unordered-set/terms/empty/empty.md b/content/cpp/concepts/unordered-set/terms/empty/empty.md new file mode 100644 index 00000000000..0619d77119c --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/empty/empty.md @@ -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 +#include +using namespace std; + +int main() { + unordered_set 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 +#include +#include +using namespace std; + +int main() { + unordered_set 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; +} +```