difference in weight calculation

User 86810cf9fa

01-03-2006 11:02:22

Dear support,





I have testing the two methods of calculation of molecule weight with MolHandler class, with an example of molecule (see attached file molecule.mol).


This is my code:





Code:
mh = new MolHandler(myMol);


weigth = mh.calcMolWeightInDouble();


formule = mh.calcMolFormula();






the weight value returned is : 418.29936100000003





and if I use





Code:
mh.calcMolWeight()






I have : 418.2993469238281





Can you explain me why there is this difference?


I am using jchem3.1.5





Thank you very much





Severine

ChemAxon 9c0afc9aaf

01-03-2006 11:48:01

Dear Severine,





Both values are correct within the degree of precision provided by these data types.





calcMolWeightInDouble() calls the weight calculation directly.


The returned value (418.29936100000003) should be interpreted as 418.299361. The 3 at the end is actually the result of the minor mistakes Java makes when adding floating-point numbers, but as it is several orders smaller than the last meaningful number, so really negligible.





calcMolWeight() is really for backward compatibility, it actually calls calcMolWeightInDouble(), and converts the result to float.





Code:
public float calcMolWeight(){


   return (float)calcMolWeightInDouble();


}






As conversion result is loss of precision, so it returns 418.29935.





So how could you get 418.2993469238281 ?


You have most likely converted it to double again (stored in a double variable) before printing the value, and the float -> double conversion added some precision loss again.





I suggest calling the appropriate function for your data type to avoid multiple conversions.


If you store / use the value as double , please call calcMolWeightInDouble().








Kind regards,





Szilard

User 86810cf9fa

01-03-2006 13:28:04

Ho yes that's right! I verify it and I put it into a double value.





thank you for your answer.