functions.py 文件源码

python
阅读 42 收藏 0 点赞 0 评论 0

项目:NeoAnalysis 作者: neoanalysis 项目源码 文件源码
def siScale(x, minVal=1e-25, allowUnicode=True):
    """
    Return the recommended scale factor and SI prefix string for x.

    Example::

        siScale(0.0001)   # returns (1e6, '?')
        # This indicates that the number 0.0001 is best represented as 0.0001 * 1e6 = 100 ?Units
    """

    if isinstance(x, decimal.Decimal):
        x = float(x)

    try:
        if np.isnan(x) or np.isinf(x):
            return(1, '')
    except:
        print(x, type(x))
        raise
    if abs(x) < minVal:
        m = 0
        x = 0
    else:
        m = int(np.clip(np.floor(np.log(abs(x))/np.log(1000)), -9.0, 9.0))

    if m == 0:
        pref = ''
    elif m < -8 or m > 8:
        pref = 'e%d' % (m*3)
    else:
        if allowUnicode:
            pref = SI_PREFIXES[m+8]
        else:
            pref = SI_PREFIXES_ASCII[m+8]
    p = .001**m

    return (p, pref)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号