1

如何让 pythonmpmath.nstr函数保留所有尾随零mpmath.mpf('0')?看起来strip_zeros可选参数不起作用mpmath.mpf('0'),如下例所示:

Python 3.8.5 (default, Jul 28 2020, 12:59:40) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mpmath
>>> mpmath.dps=30
>>> mpmath.nstr(mpmath.mpf('0'), 10, strip_zeros=False)
'0.0'
>>> mpmath.nstr(mpmath.mpf('1'), 10, strip_zeros=False)
'1.000000000'
>>> 
4

1 回答 1

1

从源头来看,这似乎是设计使然(https://github.com/fredrik-johansson/mpmath/blob/master/mpmath/libmp/libmpf.pyto_str()

    # Special numbers
    if not s[1]:
        if s == fzero:
            if dps: t = '0.0'
            else:   t = '.0'
            if show_zero_exponent:
                t += 'e+0'
            return t
        if s == finf: return '+inf'
        if s == fninf: return '-inf'
        if s == fnan: return 'nan'
        raise ValueError

我没有解释为什么会出现这种情况,但考虑到这不是您的代码有问题,您可能应该询问项目的维护人员原因(如果您需要,也许可以找到解决方法)。

于 2020-12-14T01:30:39.113 回答