1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // Sqrt returns the square root of x.
16 func libc_sqrt(float64) float64
18 func Sqrt(x float64) float64 {
22 // The original C code and the long comment below are
23 // from FreeBSD's /usr/src/lib/msun/src/e_sqrt.c and
24 // came with this notice. The go code is a simplified
25 // version of the original C.
27 // ====================================================
28 // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
30 // Developed at SunPro, a Sun Microsystems, Inc. business.
31 // Permission to use, copy, modify, and distribute this
32 // software is freely granted, provided that this notice
34 // ====================================================
37 // Return correctly rounded sqrt.
38 // -----------------------------------------
39 // | Use the hardware sqrt if you have one |
40 // -----------------------------------------
42 // Bit by bit method using integer arithmetic. (Slow, but portable)
44 // Scale x to y in [1,4) with even powers of 2:
45 // find an integer k such that 1 <= (y=x*2**(2k)) < 4, then
46 // sqrt(x) = 2**k * sqrt(y)
47 // 2. Bit by bit computation
48 // Let q = sqrt(y) truncated to i bit after binary point (q = 1),
51 // s = 2*q , and y = 2 * ( y - q ). (1)
54 // To compute q from q , one checks whether
61 // If (2) is false, then q = q ; otherwise q = q + 2 .
64 // With some algebraic manipulation, it is not difficult to see
65 // that (2) is equivalent to
70 // The advantage of (3) is that s and y can be computed by
72 // the following recurrence formula:
75 // s = s , y = y ; (4)
80 // s = s + 2 , y = y - s - 2 (5)
83 // One may easily use induction to prove (4) and (5).
84 // Note. Since the left hand side of (3) contain only i+2 bits,
85 // it does not necessary to do a full (53-bit) comparison
88 // After generating the 53 bits result, we compute one more bit.
89 // Together with the remainder, we can decide whether the
90 // result is exact, bigger than 1/2ulp, or less than 1/2ulp
91 // (it will never equal to 1/2ulp).
92 // The rounding mode can be detected by checking whether
93 // huge + tiny is equal to huge, and whether huge - tiny is
94 // equal to huge for some floating point number "huge" and "tiny".
97 // Notes: Rounding mode detection omitted. The constants "mask", "shift",
98 // and "bias" are found in src/pkg/math/bits.go
100 // Sqrt returns the square root of x.
102 // Special cases are:
107 func sqrt(x float64) float64 {
110 case x == 0 || IsNaN(x) || IsInf(x, 1):
117 exp := int((ix >> shift) & mask)
118 if exp == 0 { // subnormal x
119 for ix&1<<shift == 0 {
125 exp -= bias // unbias exponent
128 if exp&1 == 1 { // odd exp, double x to make it even
131 exp >>= 1 // exp = exp/2, exponent of square root
132 // generate sqrt(x) bit by bit
134 var q, s uint64 // q = sqrt(x)
135 r := uint64(1 << (shift + 1)) // r = moving bit from MSB to LSB
147 if ix != 0 { // remainder, result not exact
148 q += q & 1 // round according to extra bit
150 ix = q>>1 + uint64(exp-1+bias)<<shift // significand + biased exponent
151 return Float64frombits(ix)
154 func sqrtC(f float64, r *float64) {