Newton's method)

Source: Internet
Author: User
Tags float number

Introduction

Newton's iteration method was first proposed by Sir Newton, a famous British mathematician. However, this method was not publicly published in Newton's lifetime.

 

The function of the Newton method is to use the iterative method to solve the root of the function equation. Simply put, the Newton method is the process of constantly obtaining the tangent.

For an equation such as f (x) = 0, an arbitrary solution x0 is estimated first, and then the estimated value is substituted into the original process. Because the correct solution is usually not selected, f (x) = A is available. At this time, the slope of the calculation function at x0 and the intersection of this slope with the X axis X1.

In f (x) = 0, the exact solution means that when the solution is obtained, the function value is zero (that is, the exact solution of f (x) is the zero point of the function ). Therefore, X1 is closer to the exact solution than x0. As long as we constantly update X in this way, we can obtain an infinitely close exact solution.
However, it is possible that the Newton iteration method cannot converge. For example, when a function has multiple zero points or the function is not continuous.

Examples of Newton Method 

The following is an example of using the Newton iteration method to obtain the square root. The Newton iteration method is one of the fastest known methods for square root implementation. It can be quite accurate after several iterations.

First, set the m Root of X to.

 

The following program uses the Newton Method to Solve the square root.

 1 const float EPS = 0.00001;    2 int sqrt(double x) {    3     if(x == 0) return 0;    4     double result = x; /*Use double to avoid possible overflow*/    5     double lastValue;    6     do{    7         lastValue = result;    8         result = result / 2.0f + x / 2.0f / result;    9     }while(abs(result - lastValue) > EPS);  10  return (double)result;  11  }  

Faster Method 

Document 2 mentions an atypical Newton Iteration Method that solves the square root faster than the above procedures. It is described as follows.
In December 1999, id software released an electronic game named "Thunder hammer III. It was the first game to Support Software Acceleration and achieved great success. (The Ministry of Culture included the illegal game list in 2004 due to its influence)

 

Raytheon hammer III was not the first success of id software. As early as 1993, the company was famous for its "Destroyer" series of games. In 1995, the number of "Destroyer" installations exceeded Microsoft's Windows 95. It is said that Bill Gates once considered buying id software. (Id software was later purchased by Bethesda, which launched the "elder brother scroll" series)

The success of id software is largely attributed to its founder, John carmark. Mark is also a famous programmer who is the main director of the id software game engine. Back to the Raytheon hammer mentioned earlier, Mark is an active promoter of open-source software. In 2005, he announced the source code of Raytheon hammer III. So far, people have been able to look at the source files of this game engine to see its success secrets.

The following code segment is found in a file named q_math.c.

 1 float Q_rsqrt( float number ) {    2     long i; float x2, y; const float threehalfs = 1.5F;   3     x2 = number * 0.5F;    4     y = number;    5     i = * ( long * ) &y; // evil floating point bit level hacking    6     i = 0x5f3759df - ( i >> 1 ); // what the fuck?    7     y = * ( float * ) &i;    8     y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration    9     // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed  10     #ifndef Q3_VM #  11     ifdef __linux__ assert( !isnan(y) ); // bk010122 - FPE?  12     #endif  13     #endif return y;   14 }  

This code is used to calculate the square root of a number and return its reciprocal.

It is tested to be dozens of times more efficient than the above-mentioned Newton method program. It is also several times faster than the SQRT () function of the C ++ standard library. This Code contains a strange sentence:

I = 0x5f3759df-(I> 1); // What is the fuck?

The comment for this sentence is "What the fuck ?", The translation is "what do I rely on ?"

Anyone who has been trained by the program may think about this sentence. What is it about?

The reason for this strange comment is that the author of this program (maybe Mark) does not know how to explain it clearly, or the programmer who maintains this program cannot understand this sentence completely, so it's a little hairy. In fact, its function (plus Y = y * (threehalfs-(X2 * y) is to calculate the square root.

The author does not know why.

Based on the Raytheon hammer III program, you can write more powerful square root functions than SQRT:

 1 int sqrt(float x) {    2     if(x == 0) return 0;    3     float result = x;    4     float xhalf = 0.5f*result;    5     int i = *(int*)&result;    6     i = 0x5f375a86- (i>>1); // what the fuck?    7     result = *(float*)&i;    8     result = result*(1.5f-xhalf*result*result); // Newton step, repeating increases accuracy    9     result = result*(1.5f-xhalf*result*result);   10     return 1.0f/result;   11 }

 

Newton's method)

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.