blob: 6576208f68c890391a6cd12d630804af60f5ef20 [file] [log] [blame]
// 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
export sqrt
/*
* sqrt returns the square root of its floating
* point argument. Newton's method.
*
* calls frexp
*/
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 {
temp = temp * float64(1 << (exp/2));
} else {
temp = temp / float64(1 << (-exp/2));
}
for i=0; i<=4; i=i+1 {
temp = 0.5*(temp + arg/temp);
}
return temp;
}