Sunday, December 9, 2012

How to correctly take square root in programming

If we simply call "sqrt()" function to take square root, very often we'll be caught by surprise.

The first common mistake is to not check negativity before taking square root. For example, Instead of getting 0.0 in both cases, one of the case will yield "-nan". This is because y is actually not exactly \(\sqrt{2}\), but within machine precision of \(\sqrt{2}\). Therefore, we should always check negativity before taking square root.

You may notice that, in one of the above two cases, even if the answer is not "-nan", the accuracy is quite poor, only within \(10^{-8}\) of zero, rather than within machine precision of zero. This is because although \(y^2 - 2\) is within machine precision zero, after taking square root, you'll only get square root of machine precision. This is another common mistake.

So how do we take square root accurately and error free? Assume you want to compute \(x = \sqrt{2 - y^2}\), you can do something like the following

3 comments:

Unknown said...

Weekly Current affairs

illyasnaden said...

Best 888casino Reviews USA 2021 - Mapyro
Check out 양산 출장샵 our trusted 888casino reviews. 정읍 출장안마 See ratings, reviews, complaints, 목포 출장마사지 & more for 888casino in 안동 출장마사지 the 안양 출장마사지 USA.

kenspy said...

The growing toy industry demonstrates how science and technology best sex dolls can play an important role in harnessing the power of performance and promoting overall health.

Visitors