Error propogation and calculation with units using VisAd
Code: "visad_error.py". Programming language: Python DMelt Version 1.9. Last modified: 06/28/2017. License: Pro
https://datamelt.org/code/cache/visad_error_7113.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


from visad import SI,Data,Real,RealType

def printReal(x):
   print x.toString(), "+-", (x.getError()).getErrorValue()

a=Real(10,2) # 10 +- 2 (error)
b=Real(30,5) # 30 +- 5 (error)

c=a.add(b,Data.NEAREST_NEIGHBOR, Data.INDEPENDENT)
print "a+b=", printReal(c)

c=a.divide(b,Data.NEAREST_NEIGHBOR, Data.INDEPENDENT)
print "a+b=", printReal(c)

a=Real(0.9,0.1) # 10 +- 2 (error)
c=a.cos(Data.NEAREST_NEIGHBOR, Data.INDEPENDENT)
print "acos(a)=", printReal(c)

a=Real(0.9,0.1) # 10 +- 2 (error)
c=a.pow(c,Data.NEAREST_NEIGHBOR, Data.INDEPENDENT)
print "a^4=", printReal(c)

print "use units:"
k=RealType("kelvin",SI.kelvin)
t2=Real(k,273.);
t1= Real(k,255.);
xsum =t1.add(t2);
print "sum = ",xsum," ",xsum.getUnit()



Ads help maintain this website.