summaryrefslogtreecommitdiff
path: root/src/pkg/math/sqrt.go
blob: 09bdf6b4c721217ba340821ac2ca564f3e36fddf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package math


/*
 *	sqrt returns the square root of its floating
 *	point argument. Newton's method.
 *
 *	calls frexp
 */

// Sqrt returns the square root of x.
//
// Special cases are:
//	Sqrt(+Inf) = +Inf
//	Sqrt(0) = 0
//	Sqrt(x < 0) = NaN
func Sqrt(x float64) float64 {
	if IsInf(x, 1) {
		return x;
	}

	if x <= 0 {
		if x < 0 {
			return NaN();
		}
		return 0;
	}

	y, exp := Frexp(x);
	for y < 0.5 {
		y = y*2;
		exp = exp-1;
	}

	if exp&1 != 0 {
		y = y*2;
		exp = exp-1;
	}
	temp := 0.5 * (1+y);

	for exp > 60 {
		temp = temp * float64(1<<30);
		exp = exp - 60;
	}
	for exp < -60 {
		temp = temp / float64(1<<30);
		exp = exp + 60;
	}
	if exp >= 0 {
		exp = 1 << uint(exp/2);
		temp = temp * float64(exp);
	} else {
		exp = 1 << uint(-exp/2);
		temp = temp / float64(exp);
	}

	for i:=0; i<=4; i++ {
		temp = 0.5*(temp + x/temp);
	}
	return temp;
}