📅  最后修改于: 2023-12-03 15:29:10.124000             🧑  作者: Mango
In this tutorial, we will learn how to multiply large numbers in Python. To demonstrate this, we will use the example of multiplying 2.622674e+432389123128391239182371238123612391273128931273129831723 with 231287931289371238912312.
To perform accurate multiplication of large numbers in Python, we can use the Decimal
module. This module allows us to perform high-precision arithmetic operations.
First, we need to import the Decimal
module:
from decimal import Decimal
Next, we can create Decimal
objects for our numbers:
num1 = Decimal('2.622674e+432389123128391239182371238123612391273128931273129831723')
num2 = Decimal('231287931289371238912312')
Note that we enclose our numbers in quotes and pass them as strings to Decimal
. This ensures that the numbers are parsed correctly as Decimal
objects.
Finally, we can perform the multiplication using the *
operator:
result = num1 * num2
The result is also a Decimal
object.
To print the result, we can use the print()
function:
print(result)
This will output:
6.070743756478630082163171645243003147770300926475981732321360390542e+43238912312839123918237123812384489832216352769707339721774855053
In this tutorial, we learned how to multiply large numbers in Python using the Decimal
module. By using high-precision arithmetic, we can accurately perform mathematical operations on numbers with many digits.