[Developers] is the sqrt safe for us?

Yuancheng Liu yuancheng.liu at dgb-group.com
Wed Aug 20 21:02:10 CEST 2008


Dear all,

Here is a story I would like to share with you all.

Recently I used the default function *sqrt* from c/c++ and got some 
undefined values. I was debugging my code again and again just could not 
find the error since every step seems to be correct for me.  In fact, my 
calculation involved only some zeros from one bigger double minus 
another and then I sqrted those zeros.

  After I discussed it with Kristofer, he immediately pointed out that 
was the rounding error of the computer, i.e.  the difference between the 
calculated approximation of a number and its exact mathematical value.  
Of course, in my case, I got some tiny negative numbers, which could not 
be handled by sqrt. 

  Fortunately,  in OpendTect, we do have a *Math* class in 
/Basic/math2.h to handle  this kind of problems.  Hence instead of sqrt, 
I used Math::Sqrt in my code, now, everything works just as well as it 
should be. Because of this, I have updated most of the sqrt in  
od/od3.2  version by Math::Sqrt.

So, the point is, if you have used functions like sqrt, log, acos etc in 
your  code,  instead,  you may want to use  Math::sqrt, Math::Log, 
Math::ACos etc to avoid round errors.  For more functions, please check 
Basic/math2.h.

Thank you for your time.

Best regards,

Yuancheng







More information about the Developers mailing list