All three of the below print same properties, but why does last statement print 0 electronvolt?
import scipy
from unitpy import U, Q, Unit, Quantity
def print_properties(q):
print(q.unit)
print(q.dimensionality)
print(q.dimensionless)
print(q.base_unit)
if __name__ == '__main__':
wave_length = 6.2E-6
E = scipy.constants.h * scipy.constants.c / wave_length
unitE = E * U("joule")
unitE = unitE.to("eV")
unitW = 0.1 * U("eV")
print_properties(unitE)
print_properties(unitW)
print_properties(unitE - unitW)
print(unitE) # 0.1999744579 electronvolt
print(unitW) # 0.1 electronvolt
print((unitE-unitW)) # 0 electronvolt????
I was expecting the last print statement to give 0.0999744579 electronvolt. All three of the print_properties calls gives the same result and the quantities are in electronvolt.
It's a bug of the library, as you can see in the implementation. (The
valueis1.6e-19for the eV. Because the_precisionis 10,round(value, _precision)becomes zero.) Why not report it here?Because the project is in its incubation stage, I recommend using a mature and popular library, such as Pint.