Example code of the C ++ quick Square Root Algorithm

Source: Internet
Author: User

SquareRootFloat function compute's quick sqrt algorithm, derived from Carmack's QUAKE3, is much faster than library functions

# Include <iostream>
Using namespace std;
Float SquareRootFloat (float number)
{
Long I;
Float x, y;
Const float f = 1.5F;
X = number * 0.5F;
Y = number;
I = * (long *) & y;
I = 0x5f3759df-(I> 1); // magic number
Y = * (float *) & I;
Y = y * (f-(x * y); // iteration 1 1/sqrt (number)
Y = y * (f-(x * y); // iteration 2 1/sqrt (number)
// Y = y * (f-(x * y); // iteration 3 1/sqrt (number). Please iterate multiple times if you need higher accuracy.
Return number * y;
}

Main ()
{
Float B = 9852.0;
// Cout <SquareRootFloat (B) <endl;
Float c = SquareRootFloat (B );
Float e = c * c;
Cout <e <endl;
System ("pause ");
}

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.