blob: 76b499e88048c43ceb45107e1df5719330adb121 (
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
|
// 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
*/
export func
sqrt(arg float64) float64
{
var x, temp float64;
var exp, i int;
if sys.isInf(arg, 1) {
return arg;
}
if arg <= 0 {
if arg < 0 {
return sys.NaN();
}
return 0;
}
x,exp = sys.frexp(arg);
for x < 0.5 {
x = x*2;
exp = exp-1;
}
if exp&1 != 0 {
x = x*2;
exp = exp-1;
}
temp = 0.5 * (1+x);
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=i+1 {
temp = 0.5*(temp + arg/temp);
}
return temp;
}
|