math.sqrt - Where can I find the source code for Java's Square Root function? -
I know that Math.sqrt calls StrictMath.sqrt (double A), I used to calculate the actual It wanted to see the code.
When you install a JDK, then the source code of the standard library should be found inside the src. Could. Zip . This will not help you for StrictMath , however, StrictMath.sqrt (double) has been implemented as follows:
Public static original double sqrt (double A); So this is actually just an original call and Java can be implemented differently on different platforms.
However, StrictMath states:
To ensure the portability of Java programs, this package is essential for the definition of some numerical functions That they produce the same results as some published algorithms. These algorithms are available in the form of the famous Network Library
netlibas a "Freely Distributable Math Library" package. These algorithms, which are written in C programming language, can be understood that Java floating-point is executed with all floating-point operations after arithmetic rules.The Java Mathematical Library is defined in relation to fdlibm version 5.3. Where fdlibm provides more than one definition for a function (such as ACOS), use the "IEEE 754 Core Function" edition (the name of a file whose name starts with E-letter) methods which require fdlibm semantics Is sin, cos, tan, asin, eko, AN, ap, log, log 10, cbr, ana2, pau, cih, cosh, tan, hypot, apm1, and log1p.
Therefore, by finding the appropriate version of the source, you should also get the exact implementation used by Java (and mandatory by specification here).
Implementation used by fdlibm
Fixed const double one = 1.0, small = 1.0-300; Double jade; Int sign = (int) 0x80000000; Unsigned R, T1, S1, I1, Q1; Int ix0, s0, q, m, t, i; Ix0 = __HI (x); High words of / * x * / ix1 = __lo (x); / * Less words of * * / / * Keep in mind the information given by Info and Nyan * / if ((ix0 and 0x7ff00000) == 0x7ff00000) {return x * x + x; Keep in mind * / * Sqrt (NaN) = nan, sqrt (+ inf) = + inf, sqrt (-inf) = sNaN * /} / * zero * / if (ix0 & lt; = 0) {if ((( Ix0 & amp;; (~ sign) | ix1) == 0) {return x; / * Sqrt (+ - 0) = + -0 * /} and if (ix0 and lt; 0) {returns (x-x) / (x-x); / * Sqrt (-ve) = sNaN * /}} / * Normal x * / m = (ix0>> 20); If (m == 0) {/ * Submatic X * / while (ix0 == 0) {m - = 21; Ix0 | = (Ix1>; 11;); Ix1 & lt; & Lt; = 21; } For (i = 0; (ix0 and 0x00100000) == 0; i ++) {ix0 31); Ix1 + = ix1; } M & gt; & Gt; = 1; / * M = [m / 2] * / / * bit * bit = / ix0 + = ix0 + ((ix1 and sign)> 31); Generate sqrt (x) bit; Ix1 + = ix1; Q = q1 = s0 = s1 = 0; / * [Q, q1] = sqrt (x) * / r = 0x00200000; / * R = moving bit to right to left * / while (R! = 0) {t = s0 + r; If (T & LT; = ix0) {s0 = t + r; Ix0 - = T; Q + = r; } Ix0 + = ix0 + ((ix1 and sign)> 31); Ix1 + = ix1; R & gt; & Gt; = 1; } R = Icon; While (R! = 0) {t1 = s1 + r; T = s0; If ((T & lt; ix0) || ((T == ix0) & amp; (T1 and LT; = ix1))) {S1 = T1 + R; If (((T1 and sign) == symbol) & amp; (S1 and signature) == 0) {s0 + = 1; } Ix0 - = T; If (ix1 & lt; t1) {ix0 - = 1; } Ix1 - = T1; Q1 + = r; } Ix0 + = ix0 + ((ix1 and sign)> 31); Ix1 + = ix1; R & gt; & Gt; = 1; } / * Spherical direction * / if ((ix0 | ix1)! = 0) {z = one - small; / * Trigger irregular flag * / if (z> = =) {z = one + small; If (q1 == (unsigned) 0xffffffff) {q1 = 0; Q + = 1; }} And if (z> a) {if (q1 == (unsigned) 0xfffffffe) {q + = 1; } Q1 + = 2; } And q1 + = (q1 and 1); }} Ix0 = (q>> 1) + 0x3fe00000; Ix1 = x1 1> & Gt; 1; If ((Q & amp; 1) == 1) ix1 | = Icon; Ix0 + = (M & lt; & lt; 20); __HI (z) = ix0; __LO (Zed) = ix1; Return z;
Comments
Post a Comment