在python中用gmpy实现高精度计算

https://code.google.com/p/gmpy/wiki/UsingGmpy2AndMpfr 

点击打开链接

Using gmpy2 and mpfr. 
Documentation
Updated  Jul 5, 2012 by  casevh

Using gmpy2 and mpfr

Overview

MPFR is a library that provides correctly rounding multiple precision floating-point aithmetic. In addition to supporting the basic arithmetic operations, MPFR support a wide variety of both elementary and advanced functions. The precision and exponent range can be changed to suit the needs of a particular calculation.

Examples

Some trivial examples.

>>> import gmpy2
>>> gmpy2.mpfr("1.2")
mpfr('1.2')
>>> gmpy2.sqrt(gmpy2.mpfr("3.2"))
mpfr('1.7888543819998317')
>>> gmpy2.sin(gmpy2.mpfr(".5"))
mpfr('0.47942553860420301')
>>> 

help(gmpy2) will display all the online documentation. Help for an individual function can also be displayed.

>>> help(gmpy2.sin)
Help on built-in function sin in module gmpy2:

sin(...)
    sin(x): returns sine of x; x in radians.

>>>

gmpy2 uses a context manager to control the precision and exponent range, rounding mode, track the occurrence of any "exceptions", and to optionally raise a Python exception.

>>> gmpy2.get_context()
context(subnormalize=False,
        precision=53,
        round=RoundToNearest,
        emax=1073741823, emin=-1073741823,
        trap_underflow=False, underflow=False,
        trap_overflow=False, overflow=False,
        trap_inexact=False, inexact=True,
        trap_invalid=False, invalid=False,
        trap_erange=False, erange=False,
        trap_divzero=False, divzero=False)
>>>

The default precision is 53 bits, which corresponds to approximately 16 decimal digits. To change the working precision, just change the precision attribute of the current context.

>>> gmpy2.get_context().precision = 100
>>> gmpy2.sqrt(gmpy2.mpfr("3.2"))
mpfr('1.7888543819998318067780219999297',100)
>>> gmpy2.get_context()
context(subnormalize=False,
        precision=100,
        round=RoundToNearest,
        emax=1073741823, emin=-1073741823,
        trap_underflow=False, underflow=False,
        trap_overflow=False, overflow=False,
        trap_inexact=False, inexact=True,
        trap_invalid=False, invalid=False,
        trap_erange=False, erange=False,
        trap_divzero=False, divzero=False)
>>>

In the previous example, the precision was increased to 100 bits and then the sqrt(3.2) was calculated. Since sqrt(3.2) can not be calculated exactly, the inexact flag is set. The flags accumulate all the exceptional conditions that occur during a sequence of calculations. To clear all the flags, use gmpy2.get_context().clear_flags(). To clear an individual flag, use gmpy2.get_context().inexact = False.

When exceptional conditions are encountered, MPFR returns "nan", "inf", or "-inf" and sets the appropriate exception flag. This is the default behavior in gmpy2. Exceptions can be enabled by setting the corresponding trap attribute of the context.

>>> gmpy2.get_context()
context(subnormalize=False,
        precision=100,
        round=RoundToNearest,
        emax=1073741823, emin=-1073741823,
        trap_underflow=False, underflow=False,
        trap_overflow=False, overflow=False,
        trap_inexact=False, inexact=False,
        trap_invalid=False, invalid=False,
        trap_erange=False, erange=False,
        trap_divzero=False, divzero=False)
>>> 1/gmpy2.mpfr(0)
mpfr('inf')
>>> gmpy2.get_context()
context(subnormalize=False,
        precision=100,
        round=RoundToNearest,
        emax=1073741823, emin=-1073741823,
        trap_underflow=False, underflow=False,
        trap_overflow=False, overflow=False,
        trap_inexact=False, inexact=False,
        trap_invalid=False, invalid=False,
        trap_erange=False, erange=False,
        trap_divzero=False, divzero=True)
>>> gmpy2.get_context().trap_divzero = True
>>> 1/gmpy2.mpfr(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
gmpy2.DivisionByZeroError: 'mpfr' division by zero
>>> 

By setting gmpy2.get_context().trap_erange = True, comparisons involving "nan" will generate an exception.

>>> gmpy2.get_context().trap_erange = True
>>> gmpy2.mpfr('nan') == gmpy2.mpfr('nan')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
gmpy2.RangeError: comparison with NaN

Setting trap_underflow will generate an exception if a calculation underflows to 0. Setting trap_overflow will generate an exception if the result of a calculation exceeds the exponent range. Setting trap_invalid will generate an exception if an invalid operation, saygmpy2.asin(34), is attempted.

get_context() creates a reference to the current context. A new context object can be created with context(). To activate a context object, use set_context(context()). The "with ..." statement can also be used.

>>> with gmpy2.local_context() as ctxt:
...     for i in range(4):
...         ctxt.precision += 10
...         print(gmpy2.mpfr(gmpy2.sqrt(2.0)))
... 
1.4142135623730950488
1.4142135623730950488017
1.4142135623730950488016887
1.4142135623730950488016887241
>>> 

Another example ...

>>> gmpy2.mpfr("1.2345") + gmpy2.mpfr("3.56789")
mpfr('4.8023899999999999999999999999987',100)
>>> gmpy2.mpfr("1.000000000000000000001") + gmpy2.mpfr("3.000000000000000000007")
mpfr('4.0000000000000000000079999999986',100)
>>>

The last examples illustrate that MPFR uses a binary, or power of 2, representation. Just like 1/3 = 0.3333... can not be represented exactly with decimal number, most decimal numbers can not be represented exactly in binary format. If you have a need for exact decimal math, financial calculation, for example, you should look at the Decimal module included with Python.

Values can be displayed to a specific number of digits.

>>> format(gmpy2.mpfr("1.2345") + gmpy2.mpfr("3.56789"), ".20")
'4.80239000000000000000'
Comment by jameski...@gmail.com Jul 5, 2012

1st line of 4th grey box: shouldn't gmpy2.context().precision = 100 instead be gmpy2.get_context().precision = 100

Hint: You can use  Wiki Syntax.
Enter a comment:



Python中的整数类型可以表示非常大的整数(在理论上,它们可以无限大),这使得Python成为一种非常适合高精度计算的语言。 Python中的高精度计算可以使用内置的整数类型int来完成。我们可以直接使用int来存储和计算大整数,而不需要考虑位数的限制。例如: ``` a = 123456789012345678901234567890 b = 987654321098765432109876543210 c = a + b print(c) ``` 输出结果为: ``` 1111111111111111111111111111111 ``` 当然,这并不意味着Python可以无限制地进行高精度计算。在实际使用中,由于内存的限制,Python高精度计算也有其局限性。 除了使用内置的整数类型int,Python还提供了一些第三方库,如gmpy2和decimal,可以更加高效地进行高精度计算。这些库可以提供更多的功能,如支持小数、复数等类型的计算,并且可以更好地处理精度和舍入问题。 使用gmpy2库进行高精度计算的示例: ``` import gmpy2 a = gmpy2.mpz('123456789012345678901234567890') b = gmpy2.mpz('987654321098765432109876543210') c = gmpy2.add(a, b) print(c) ``` 输出结果为: ``` 1111111111111111111111111111100 ``` 使用decimal库进行高精度计算的示例: ``` from decimal import Decimal a = Decimal('123456789012345678901234567890') b = Decimal('987654321098765432109876543210') c = a + b print(c) ``` 输出结果为: ``` 1111111111111111111111111111100 ``` 总之,Python非常适合进行高精度计算,无论是使用内置类型还是第三方库,都可以轻松地完成高精度计算任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值