pythonuncertainty

How to display the result of a calculation with ufloats more precisely than two digits past decimal point?


I'm rather new to python and ran into a problem working with uncertainties. Whenever I try to do a calculation with uncertainties using ufloats, the result is cut off after the second digit after decimal point, even if the ufloats put into the equation have more than two digits after decimal point. Is there a way to force a more precise output? Example below.

a = ufloat(0.5278,0.1472)
b = ufloat(0.6432,0.1347)
c = (a-b)/b
print('c = ', c)

Despite a and b being decimal numbers with four digits after decimal point, I get the result c = -0.18+/-0.29.

I tried to fix it using round(c, 4), which gave out an error message:

TypeError: type AffineScalarFunc doesn't define round method

Any help is appreciated, it may be a stupid question, but I didn't find a solution despite thorough research online.


Solution

  • Format it during printing

    from uncertainties import ufloat
    
    a = ufloat(0.5278, 0.1472)
    b = ufloat(0.6432, 0.1347)
    c = (a - b) / b
    
    print(f'c = {c:.4f}')  
    

    Result:

    c = -0.1794+/-0.2862