Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading