From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <36DED148.2FC2AF00@frankfurt.netsurf.de> Date: Thu, 04 Mar 1999 19:30:32 +0100 From: Joachim Wesner Subject: Re: sqrt performance In-Reply-To: <199903040515.WAA19477@ellpspace.math.ualberta.ca> Michal Jaegermann wrote: > > esci wrote: > > > > I'm currently looking into using a table driven Taylor's series expansion. > > Hint's and suggestions *very* welcome. I need to do this over the range > > from 0 to 100,000,000 w/ the roots off by less than 1 > > If you reduce that to an interval [0,1) this would mean that you > need 10 decimal places. A decent polynomial approximation plus one > step of a Newton iteration should bring you in that range, I think. Not necessarily, at least not for high (aka. full 16 digits). There is a reason that there is no polynomial based approximation for sqrt in libffm. First, you want uniform *relative* precision for the function over the full range, so 0..1 won't work. However, you can take acount of the floating point format and only operate on the mantissa and 0.25..1 or 0.5..1 using frexp and ldexp (which however, are notoriously ineffective when using the C lib routine, clearly, you NEED assembler here). Unfortunatelly, sqrt is notoriusly ugly in approximating even for small intervalls, you need much higher orders (and large coefficients for the high orders, i.e. bad with respect to numerical stability) compared to say sin(), which is much more well behaved. However, assuming you go for 0.5..1 where you probably need to adjust the result by sqrt(0.5) (further slowdown, you need to test it and decide) and you want 1e-10 final *relative* precision, you need better 1e-5 rel. precision before the final Newton step. If anybody wants to give it a try, here are the coefficients for a (nearly) minimax relative prec. polynomial: For better 1e-5 rel. precision from 0.5..1 you already need a 5th order polynomial with the coefficients A 0: 2.07200449623728886e-001 A 1: 1.45700060266363978e+000 A 2: -1.34960056099104825e+000 A 3: 1.11173630566745442e+000 A 4: -5.38779078465377500e-001 A 5: 1.12443059720350031e-001 (i.e. A0 + x*A1 + x*x*A2 ...) which gives you better than ~ 1.4e-6 rel. precision. If you go for only forth order, you get A 0: 2.30280650419599074e-001 A 1: 1.29379894493964541e+000 A 2: -8.94996784387289337e-001 A 3: 4.88028708781137845e-001 A 4: -1.17117604514064883e-001    however, max. rel. error here is just above the range, i.e. 1.1e-5 Compared to that, for 1e-8 or better 1e-9 initial rel. precision for a full prec. version, you need >= 9th order, slow to evaluate on a machine with pipled floating point unit. You can "vectorize" it by some tricks, that are actually usued in libffm, but you have to further sacrifice some precision for that. I would be interested in the result you get, however, I fear, you won't be able to beat the new libcpml speed !? Best wishes, Joachim