# NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

## Easy Java Simulations (2001- ) => Questions related to EJS => Topic started by: lookang on January 25, 2012, 03:45:47 pm

 Title: is it possible to eliminate rounding off error in this simulation calculation? Post by: lookang on January 25, 2012, 03:45:47 pm Dear Prof Hwang!is it possible to eliminate rounding off error in this simulation calculation?http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=2067.msg8920#msg8920notice the L1 = 10.416 = 10.42L2 = 9.393 = 9.38delta L = 10.33 = 10.3but 10.42-9.38 =! 10.3 but actually = 10.4 Title: Re: is it possible to eliminate rounding off error in this simulation calculation? Post by: Fu-Kwun Hwang on January 26, 2012, 08:38:45 am From the attached picture:L1=10.416L2=9.383So L1-L2=1.033 It seems that you round of the above result to 2 significant figures.So S1P=10.42 , S2P=9.38 and you round off |S1P-S2P} from the above |1.033| value, so you get 1.03If you want to get 1.04 you should calculate difference from the round-off values instead.e.g.double dv;public float  roundoff(double v){ if(v>0)dv=0.5; else if(v<0)dv=-0.5; else dv=0; return (int)(v*100+dv)/100.;}roundoff(L1) will give you 10.42roundoff(L2)will give you 9.38roundof(L1-L2)will give you 1.03roundoff(L1)-roundoff(L2) will give you 1.04This is an opportunity to teach students about the meaning of significant value (the last digit might be off).I would not change the above result, but I will ask student why it is off (provide opportunity for student to think about this problem).It could have more education value if we take the opportunity to invite student to think/learn:There is always error due to numerical simulation.And the physics experiment always contain error, too! It is a nature of physics. Title: Re: is it possible to eliminate rounding off error in this simulation calculation? Post by: lookang on January 26, 2012, 07:36:28 pm i agree!i will not modify the simulation to have round-off effect.thanks for the Ejs tip on round off and the greater learning of nature of precision in physics!