C + + skillfully open the square of the implementation code _c language

Source: Internet
Author: User

You can guess a number, such as 1.5, and then divide by 2 by this number. If we're right, the result of division is necessarily the same as the number we're guessing. The more accurate we speculate, the closer the result of division is to the number of guesses.

According to this principle, as long as we take the guessing number and the median of the feedback number as a new guess number, it will be closer to the answer! This method of calculation is called "iterative method".

Copy Code code as follows:

Double n = 2;
Double A = 0;
Double b = n;
while (Fabs (a-b) >1e-15)//when calculated to number and guess number within a certain error output guess number
{
A = (a+b)/2; The guessing number is half of n
b = n/A;
}
printf ("%f\n", a);

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.