-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqrt.go
More file actions
38 lines (31 loc) · 893 Bytes
/
sqrt.go
File metadata and controls
38 lines (31 loc) · 893 Bytes
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
package exact
// Sqrt computes the square root of x.
// The precision is DefPrecision.
// To use arbitrary precision, see Sqrtp.
// Note: this function is orders of magnitude slow than math.Sqrt,
// but the result can be made as precise as you wish by using
// Sqrtp.
func Sqrt(x Rat) Rat {
return Sqrtp(x, DefPrecision)
}
// Sqrtp computes the square root of x using precision prec.
// Note: Depending on the value of prec, this function can
// take hours, days, years, a life time, ..., to complete.
func Sqrtp(x, prec Rat) Rat {
if x.P.Cmp(zero) == 0 {
return Zero()
}
g := One() // first guess
for !closeEnough(Div(x, g), g, prec) {
g = betterGuess(x, g)
}
return g
}
// make g = (g+(x/g))/2
func betterGuess(x, g Rat) Rat {
return Mul(Add(g, Div(x, g)), NewRat(1, 2))
}
// abs(a-b) < precision
func closeEnough(a, b, prec Rat) bool {
return Lt(Abs(Sub(a, b)), prec)
}