简体   繁体   English

在python中以科学记数法打印非常大的长度

[英]Print extremely large long in scientific notation in python

Is there a way to get python to print extremely large longs in scientific notation? 有没有办法让python以科学记数法打印极大的长片? I am talking about numbers on the order of 10^1000 or larger, at this size the standard print "%e" % num fails. 我说的是大约10 ^ 1000或更大的数字,在这个尺寸下标准打印“%e”%num失败。

For example: 例如:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "%e" % 10**100
1.000000e+100
>>> print "%e" % 10**1000
Traceback (most recent call last):
  File "", line 1, in 
TypeError: float argument required, not long

It appears that python is trying to convert the long to a float and then print it, is it possible to get python to just print the long in scientific notation without converting it to a float? 似乎python试图将long转换为float然后打印它,是否有可能让python只用科学记数法打印长而不将其转换为浮点数?

gmpy to the rescue...: gmpy救援......:

>>> import gmpy
>>> x = gmpy.mpf(10**1000)
>>> x.digits(10, 0, -1, 1)
'1.e1000'

I'm biased, of course, as the original author and still a committer of gmpy , but I do think it eases tasks such as this one that can be quite a chore without it (I don't know a simple way to do it without some add-on, and gmpy 's definitely the add-on I'd choose here;-). 当然,我有偏见,作为原作者,仍然是gmpy的提交者,但我确实认为它可以简化这样的任务,如果没有它就可以完成一件苦差事(我不知道这样做的简单方法)没有一些附加组件,而且gmpy肯定我在这里选择附加组件;-)。

No need to use a third party library. 无需使用第三方库。 Here's a solution in Python3, that works for large integers. 这是Python3中的一个解决方案,适用于大整数。

def ilog(n, base):
    """
    Find the integer log of n with respect to the base.

    >>> import math
    >>> for base in range(2, 16 + 1):
    ...     for n in range(1, 1000):
    ...         assert ilog(n, base) == int(math.log(n, base) + 1e-10), '%s %s' % (n, base)
    """
    count = 0
    while n >= base:
        count += 1
        n //= base
    return count

def sci_notation(n, prec=3):
    """
    Represent n in scientific notation, with the specified precision.

    >>> sci_notation(1234 * 10**1000)
    '1.234e+1003'
    >>> sci_notation(10**1000 // 2, prec=1)
    '5.0e+999'
    """
    base = 10
    exponent = ilog(n, base)
    mantissa = n / base**exponent
    return '{0:.{1}f}e{2:+d}'.format(mantissa, prec, exponent)

Here's a solution using only standard library: 这是仅使用标准库的解决方案:

>>> import decimal
>>> x = 10 ** 1000
>>> d = decimal.Decimal(x)
>>> format(d, '.6e')
'1.000000e+1000' 

Try this: 尝试这个:

>>> def scientific_notation(v): # Note that v should be a string for eval()
        d = Decimal(eval(v))
        e = format(d, '.6e')
        a = e.split('e')
        b = a[0].replace('0','')
        return b + 'e' + a[1]

>>> scientific_notation('10**1000')
'1.e+1000'
>>> scientific_notation('10**1000')
'1.e+1000'
>>> sc('108007135253151**1000') # Even handles large numbers
'2.83439e+14033'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM