From 808383592f19541374b380b7c3434d8aadfb9794 Mon Sep 17 00:00:00 2001 From: Kate Suraev Date: Fri, 7 Nov 2025 21:37:01 +1100 Subject: [PATCH 1/2] [Term Entry] C# Math Functions: BigMul() --- .../math-functions/terms/bigmul/bigmul.md | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 content/c-sharp/concepts/math-functions/terms/bigmul/bigmul.md diff --git a/content/c-sharp/concepts/math-functions/terms/bigmul/bigmul.md b/content/c-sharp/concepts/math-functions/terms/bigmul/bigmul.md new file mode 100644 index 00000000000..c012a9ac38b --- /dev/null +++ b/content/c-sharp/concepts/math-functions/terms/bigmul/bigmul.md @@ -0,0 +1,94 @@ +--- +Title: '.BigMul()' +Description: 'Returns the full product of two integers, preventing overflow.' +Subjects: + - 'Computer Science' + - 'Code Foundations' +Tags: + - 'Methods' + - 'Numbers' + - 'Arithmetic' +CatalogContent: + - 'learn-c-sharp' + - 'paths/computer-science' +--- + +The **`Math.BigMul()`** method multiplies two integers of the same type and returns the full product as a larger data type to prevent overflow. + +## Syntax + +```pseudo +Math.BigMul(x, y); +``` + +**Parameters:** + +- `x`, `y`: The integers to be multiplied. These can be of type `int`, `uint`, `long`, or `ulong`. + +**Return value:** + +Returns the full product of `x` and `y` as a single 64-bit or 128-bit integer, depending on the input type: + +- If `x` and `y` are of type `int` or `uint`, the return type is `long` and `ulong`, respectively. +- If `x` and `y` are of type `long` or `ulong`, the return type is `Int128` and `UInt128`, respectively. + +> **Note:** The `Int128` and `UInt128` types are available starting in .NET 9. For earlier versions, use the three-parameter overload of `Math.BigMul()` to split the result into high and low 64-bit parts. + +## Example: Basic Usage + +The following example computes the full product of two `int` values and two `long` values: + +```cs +using System; + +public class Example { + public static void Main (string[] args) { + // Integer values (32-bit) + int intX = 100000; + int intY = 500000; + + // Long values (64-bit) + long longX = -3000000000; + long longY = 4000000000; + + // Compute the products + long intResult = Math.BigMul(intX, intY); + Int128 longResult = Math.BigMul(longX, longY); + + // Display results + Console.WriteLine($"Math.BigMul({intX}, {intY}) = {intResult}"); + Console.WriteLine($"Math.BigMul({longX}, {longY}) = {longResult}"); + } +} +``` + +This example outputs the following: + +```shell +Math.BigMul(100000, 500000) = 50000000000 +Math.BigMul(-3000000000, 4000000000) = -12000000000000000000 +``` + +## Codebyte Example + +The following example demonstrates the use of `Math.BigMul()` to calculate the product of two integers that would normally cause an overflow with standard multiplication: + +```codebyte/csharp +using System; + +public class Example { + public static void Main (string[] args) { + int x = 200000; + int y = 300000; + + // Normal multiplication (overflow) + int normalResult = x * y; + + // Math.BigMul() (no overflow) + long bigMulResult = Math.BigMul(x, y); + + Console.WriteLine($"Normal multiplication (overflow): {normalResult}"); + Console.WriteLine($"BigMul (no overflow): {bigMulResult}"); + } +} +``` From 13cd00b3c1bbdd23422f09effe61500fab6d46b0 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 8 Nov 2025 15:53:20 +0530 Subject: [PATCH 2/2] minor example error fix --- .../math-functions/terms/bigmul/bigmul.md | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/content/c-sharp/concepts/math-functions/terms/bigmul/bigmul.md b/content/c-sharp/concepts/math-functions/terms/bigmul/bigmul.md index c012a9ac38b..99aacf7e725 100644 --- a/content/c-sharp/concepts/math-functions/terms/bigmul/bigmul.md +++ b/content/c-sharp/concepts/math-functions/terms/bigmul/bigmul.md @@ -1,6 +1,6 @@ --- Title: '.BigMul()' -Description: 'Returns the full product of two integers, preventing overflow.' +Description: 'Multiplies two integer values and returns the full extended result to prevent overflow.' Subjects: - 'Computer Science' - 'Code Foundations' @@ -13,7 +13,7 @@ CatalogContent: - 'paths/computer-science' --- -The **`Math.BigMul()`** method multiplies two integers of the same type and returns the full product as a larger data type to prevent overflow. +The **`Math.BigMul()`** method multiplies two integer values and returns the full extended result to avoid overflow. It ensures that the product fits within a larger data type when the operands are 32-bit or 64-bit integers. ## Syntax @@ -32,32 +32,25 @@ Returns the full product of `x` and `y` as a single 64-bit or 128-bit integer, d - If `x` and `y` are of type `int` or `uint`, the return type is `long` and `ulong`, respectively. - If `x` and `y` are of type `long` or `ulong`, the return type is `Int128` and `UInt128`, respectively. -> **Note:** The `Int128` and `UInt128` types are available starting in .NET 9. For earlier versions, use the three-parameter overload of `Math.BigMul()` to split the result into high and low 64-bit parts. +> **Note:** The `Int128` and `UInt128` types were introduced in .NET 7. In earlier versions, `Math.BigMul(long, long, out long high)` can be used to obtain the high and low 64-bit parts of the product. ## Example: Basic Usage -The following example computes the full product of two `int` values and two `long` values: +In this example, the full product of two `int` values is calculated without overflow: ```cs using System; public class Example { - public static void Main (string[] args) { + public static void Main() { // Integer values (32-bit) int intX = 100000; int intY = 500000; - // Long values (64-bit) - long longX = -3000000000; - long longY = 4000000000; - - // Compute the products + // Compute the product safely long intResult = Math.BigMul(intX, intY); - Int128 longResult = Math.BigMul(longX, longY); - // Display results Console.WriteLine($"Math.BigMul({intX}, {intY}) = {intResult}"); - Console.WriteLine($"Math.BigMul({longX}, {longY}) = {longResult}"); } } ``` @@ -66,7 +59,6 @@ This example outputs the following: ```shell Math.BigMul(100000, 500000) = 50000000000 -Math.BigMul(-3000000000, 4000000000) = -12000000000000000000 ``` ## Codebyte Example