plus/minus operator for python ±

前端 未结 7 626
遇见更好的自我
遇见更好的自我 2020-12-31 02:20

I am looking for a way to do a plus/minus operation in python 2 or 3. I do not know the command or operator, and I cannot find a command or operator to do this.

Am I

7条回答
  •  攒了一身酷
    2020-12-31 02:57

    Another possibility: uncertainties is a module for doing calculations with error tolerances, ie

    (2.1 +/- 0.05) + (0.6 +/- 0.05)    # => (2.7 +/- 0.1)
    

    which would be written as

    from uncertainties import ufloat
    
    ufloat(2.1, 0.05) + ufloat(0.6, 0.05)
    

    Edit: I was getting some odd results, and after a bit more playing with this I figured out why: the specified error is not a tolerance (hard additive limits as in engineering blueprints) but a standard-deviation value - which is why the above calculation results in

    ufloat(2.7, 0.07071)    # not 0.1 as I expected!
    

提交回复
热议问题