Decompose a float into mantissa and exponent in base 10 without strings

前端 未结 2 1161
生来不讨喜
生来不讨喜 2021-01-02 00:02

Are there functions in the Python library or numpy that take a float as input and return its decimal scientific notation decomposition, i.e. mantissa and exponent? Or is the

2条回答
  •  猫巷女王i
    2021-01-02 00:38

    I'm hoping there's a better answer but I came up with

    from math import floor, log10
    
    def fexp(f):
        return int(floor(log10(abs(f)))) if f != 0 else 0
    
    def fman(f):
        return f/10**fexp(f)
    

提交回复
热议问题