BigInteger.Multiply Method
Returns the product of two BigInteger values.
Assembly: System.Numerics (in System.Numerics.dll)
Parameters
- left
- Type: System.Numerics.BigInteger
The first number to multiply.
- right
- Type: System.Numerics.BigInteger
The second number to multiply.
The Multiply method is implemented for languages that do not support operator overloading. Its behavior is identical to multiplication using the multiplication operator. In addition, the Multiply method is a useful substitute for the multiplication operator when instantiating a BigInteger variable by assigning it a product that results from multiplication, as shown in the following example.
If necessary, this method automatically performs implicit conversion of other integral types to BigInteger objects. This is illustrated in the example in the next section, where the Multiply method is passed two Int64 values.
The following example tries to perform multiplication with two long integers. Because the result exceeds the range of a long integer, an OverflowException is thrown, and the Multiply method is called to handle the multiplication. Note that C# requires that you use either the checked keyword (as in this example) or the /checked+ compiler option to make sure an exception is thrown on a numeric overflow.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
<#
.SYNOPSIS
This script re-implements this MSDN Sample of
multiplying a big integer.
.DESCRIPTION
This script first tries and fails to multiple a pair of large integers. The
script catches the error and then used BigInteger.Multiply to multiply
the two big itegers.
.NOTES
File Name : Get-MultiplyBigInteger.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.multiply.aspx
.EXAMPLE
PSH [C:\foo]: .Get-MultiplyBigInteger.ps1
Too big for long, try biginteger
12,193,263,111,263,526,900
#>
# Add System.Numerics namespace
$r=[system.Reflection.Assembly]::LoadWithPartialName("System.Numerics")
# Two big numbers
$number1 = 1234567890
$number2 = 9876543210
# Try normal [long] then catch error and do biginteger
try
{
[long] $product = $number1 * $number2
}
catch
{ "Too big for long, try biginteger"
$product = New-Object System.Numerics.BigInteger
$product = [System.Numerics.BigInteger]::Multiply($number1, $number2)
$product.ToString("N0")
}
- 8/3/2010
- Thomas Lee