问题描述
如何在Python中使用 __ float128
等效项? decimal.getcontext()
应该使用什么精度?我的意思是,精度是用小数位还是位指定的?
How to work with equivalent of __float128
in Python? What precision should I use for decimal.getcontext()
? I mean, is the precision specified in decimal places or bits?
from decimal import *
getcontext().prec = # 34 or 128 ?
是否可以将给定操作的精度设置为本地,而不是全局设置 ,并带有 getcontext()。prec
?
Is it possible to set the precision "locally" for a given operation, rather than setting it "globally" with getcontext().prec
?
每个Simon Byrne评论,甚至有可能模拟 __ float128
是IEEE 754定义的十进制
?如果我想要四倍精度,我在Python中还有什么其他选择?
Per Simon Byrne comment, is it even possible to simulate __float128
as defined by IEEE 754 with Decimal
? What other options do I have in Python, if I wanted quadruple-precision?
推荐答案
我是<$ c的维护者$ c> gmpy2 。除了包装MPFR, gmpy2
还包装GMP(用于整数和有理数)和MPC(用于复杂算术)。 MPFR和MPC都使用二进制表示形式,而 Decimal
使用十进制表示形式。
I'm the maintainer of gmpy2
. In addition to wrapping MPFR, gmpy2
also wraps GMP (for integer and rational numbers) and MPC (for complex arithmetic). Both MPFR and MPC use a binary representation compared to Decimal
which uses a decimal representation.
下面是一个简单的示例显示与 float128
等价的使用。
Here is a quick example showing use of the equivalent of float128
.
>>> import gmpy2
>>> gmpy2.set_context(gmpy2.ieee(128))
>>> gmpy2.get_context()
context(precision=113, real_prec=Default, imag_prec=Default,
round=RoundToNearest, real_round=Default, imag_round=Default,
emax=16384, emin=-16493,
subnormalize=True,
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,
trap_expbound=False,
allow_complex=False)
>>> gmpy2.sin(gmpy2.mpfr("1.2"))
mpfr('0.932039085967226349670134435494826026',113)
对于2.0.x系列, gmpy2.ieee()
仅创建用于支持32位,64位或128位格式的上下文。开发部门支持的精度范围更广。
For the 2.0.x series, gmpy2.ieee()
only creates contexts for supporting 32, 64, or 128-bit formats. The development branch supports are wider range of precisions.
这篇关于相当于float128的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!