Getting wrong result using unitpy?

32 Views Asked by At

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.

1

There are 1 best solutions below

0
relent95 On

It's a bug of the library, as you can see in the implementation. (The value is 1.6e-19 for the eV. Because the _precision is 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.