On the algorithm of SQRT function

Source: Internet
Author: User
Tags square root

We usually have some operations of data operations, need to call sqrt,exp,abs and other functions, then you have not thought: how do these functional systems are implemented? Take the most commonly used sqrt function, how does the system implement this frequently called function?

Although it is possible that you do not normally think about this problem, but is the so-called "cramming, unhappy also light", you "wrinkly, stratagem", this is not too simple, with a two-point method, in an interval, each take the square of the middle number to test, if large, then try the middle of the left interval; Take the middle number of the right interval to try it again. For example sqrt (16) results, you first try (0+16)/2=8,8*8=64,64 than 16, then move to the left, try (0+8)/2=4,4*4=16 just, you get the correct results sqrt (16) = 4. Then you rinsed the program:

Then look at the difference between the performance and the accuracy of the system function (where the time unit is not the second or the millisecond, but the CPU Tick, regardless of what the unit is, the uniformity is comparable)

It can be seen that the dichotomy is exactly the same as the system's method, but the performance is hundreds of times times worse. Why is there such a big difference? Is there any better way to do this? Don't.... Oh, by the way, remember the high number of lessons we had, once the teacher taught us "Newton iterative method to quickly find square root", or this method can help us, the specific steps are as follows:

Find the approximate value of the square root a: first guess a random approximation x, and then keep x equal to the average of X and a/x, iterate six or seven times after the value of x is quite accurate.For example, I would like to ask what the root 2 equals. If I guess the result is 4, although wrong, but you can see the Newton iterative method after the value quickly approaching the root of the square root 2: (4 + 2/4)/2 = 2.25 (2.25 + 2/2.25)/ 2 = 1.56944.. (1.56944..+ 2/1.56944.)/2 = 1.42189: (1.42189..+ 2/1.42189.)/2 = 1.41423:   .... The principle of this algorithm is very simple, we just constantly use (x,f (x)) tangent to approximate the root of the equation x^2-a=0. Square root A is actually a positive real roots of x^2-a=0, the derivative of this function is 2x. That is, the tangent slope of the function at any point (x,f (x)) is 2x. So, x-f (x)/(2x) is an approximation that is closer than X. Substituting f (x) =x^2-a gets x (x^2-a)/(2x), i.e. (x+a/x)/2.

The relevant code is as follows:

float Sqrtbynewton (float x) {float val = x;//final float last;//Save the last computed value Do{last = Val;val = (val + x/val)/2;} while (ABS (Val-last) > EPs); return Val;}

Then we'll look at the performance test:

Wow, performance has improved a lot, but compared with the system function, there is still so big gap, this is why AH? Think of Ah, think for a long time still baffled its solution. Suddenly one day, I saw on the Internet a magical method, so there is today's article, nonsense not to say, look at the code first:

float invsqrt (float x) {Float Xhalf = 0.5f*x;int i = * (int*) &x;//Get bits for floating VALUE i = 0x5f375a86-(I>&G T;1); Gives initial guess y0x = * (float*) &i; Convert bits back to Floatx = x* (1.5f-xhalf*x*x); Newton step, repeating increases accuracyx = x* (1.5f-xhalf*x*x); Newton step, repeating increases accuracyx = x* (1.5f-xhalf*x*x); Newton step, repeating increases Accuracyreturn 1/x;}

And then we'll look at the performance test for the last time:

This is really a qualitative change, the result is even better than the system ...

On the algorithm of SQRT function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.