The calculation of the square root is also implemented in the Linux kernel, just like the SQRT function in the MATH.H math library.
The formula for the square root is defined:
if a non-negative
xof theSquareequals
a, i.e.,, then this non-negative
xcalled
aof thesquare root of arithmetic.
athe square root of the arithmetic is recorded as, read as " radical
a",
acalled Radicals (radicand). Ask for a non-negative number
athe square root of the operation is calledOpen Square. Conclusion: The larger the radicals, the greater the square root of the corresponding arithmetic (which is true for all positive numbers). aPositiveIf there is a square root, then there must be two, they are mutuallyopposite number. Obviously, if we know one of these two square roots, then we can get another square root of it in time based on the concept of the opposite number. Haha, elementary school students understand, do not explain, directly to see the code:like, take the code out of the kernel:
#include <stdio.h> #ifdef config_64bit#define bits_per_long 64#else#define bits_per_long 32#endif/** * int_sqrt- Rough approximation to SQRT * @x:integer of which-calculate the sqrt * * A very rough approximation to the sqrt () func tion. */unsigned long int_sqrt (unsigned long x) {unsigned long op, res, one;op = x;res = 0;one = 1UL << (bits_per_long-2) ; while (one > op) one >>= 2;while (one! = 0) {if (op >= res + one) {op = op-(res + one); res = res + 2 * o NE;} Res/= 2;one/= 4;} return res;} int main (void) {printf ("%d\n", int_sqrt (+)); return 0;}
Operation Result:
C-language Linux kernel implementation of square root computing algorithm