Written question: Ask for a number of the root, such as the square root 2

Source: Internet
Author: User
Tags abs lowercase square root
Title:The root of a number, such as the square root 2, is required to be reserved to 10 digits after the decimal point.
Solution One:It is equivalent to seeking a number N of the root, we use the dichotomy method to calculate, shrinking the range, but double, float can not be directly, and finally if the difference between Mid*mid and N is not more than a specified minimum value. So the mid we're asking for is the value we get. Then we'll print it according to the 10 digits after the decimal point. The code is as follows:
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
void Kaifang (double n,double accuracy);
void Main ()
	double n=2;
	Double accuracy=1e-10;
	Kaifang (n,accuracy);

void Kaifang (double n,double accuracy)
	if (n<0) return;
	Double low=0;
	Double high=n;
	Double kf=0;
	while (Low

Note: Setprecision is also a C + + operator contained in the namespace Iomanip, which is the function of setting the number of digits after the decimal point of a floating point;
Setprecision (2) means the precision of the decimal point output, that is, the number of digits to the right of the decimal point is 2,c++ the default stream output value of the valid bit is 6. #include <iomanip>
It is the I/O flow control header file, just like the formatted output in C. Here are some common control functions:
Dec base is 10 equivalent to "%d"
Hex base is 16 equivalent to "%x"
Oct has a base of 8 equivalent to "%o" Setioflags (ios::fixed) fixed floating point display
Setioflags (ios::scientific) index indicates
Setiosflags (ios::left) Align Left
Setiosflags (ios::right) Align Right
Setiosflags (IOS::SKIPWS ignores leading whitespace
Setiosflags (ios::uppercase) 16 Decimal Capital Output
Setiosflags (ios::lowercase) 16 binary lowercase output
Setiosflags (ios::showpoint) force display decimal point
Setiosflags (ios::showpos) force display symbols
#include <iostream>
#include <iomanip>
using namespace Std;
int main ()
cout<<12345.0<<endl;//Output "12345"
Cout<<setiosflags (ios::fixed) <<setprecision (3) <<1.2345<<endl; output "1.235"
Cout<<setiosflags (ios::scientific) <<12345.0<<endl;//output "1.234500e+004"
Cout<<setprecision (3) <<12345.0<<endl;//output "1.235e+004"
return 0;

Solution Two:Using Newton's Iterative method, any integer x, I guess it's square root is y, if wrong or accuracy is not accurate, then I make y = (x+x/y)/2. As the loop repeats, Y will approximate the square root of x infinitely. For example, I would like to ask what the root 2 equals. If I guessed the result to be 4, although the wrong is outrageous, but you can see the use of 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 code is as follows:
Double newtoniteration (double n,double accuracy)
	Double y=1;//can be arbitrarily evaluated (except 0)
	long m=0;
	while (ABS ((y*y)-N) >accuracy)
		y= (y+ (n/y))/2;
	printf ("%.10f\n", y);//Use the original C to print out the decimal point after 10 bits
	cout<<setiosflags (ios::fixed) <<setprecision (Ten) << y<<endl;//using C + + operators to print out 10 digits after
	the decimal point cout<< "while number:" <<m;
	return y;

Finally, we compare the number of times that two algorithms execute while loops, and give the complete code as follows:
#include <iostream> #include <math.h> #include <iomanip> using namespace std;
void Kaifang (double n,double accuracy);
Double newtoniteration (double n,double accuracy);
	void Main () {double n=2;
	Double accuracy=1e-10;
	Kaifang (n,accuracy);
Newtoniteration (n,accuracy);
	} void Kaifang (double n,double accuracy) {if (n<0) return;
	Double low=0;
	Double high=n;
	Double kf=0;
	Long m=0;
		while (Low 

The result of the operation is:

From the results can be seen Newton method of computational efficiency, I also tried the other Y value, if the Y value is 4 or 3 o'clock, still while4 times, even if it is 8, but only while 7 times, compared to 29 times of the dichotomy, the efficiency is much higher.

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.