Skip to content

Commit

Permalink
all: a few cleanups
Browse files Browse the repository at this point in the history
Fixes ALTree#16
Fixes ALTree#5
  • Loading branch information
ALTree committed May 4, 2016
1 parent ef5a904 commit 27fd314
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
12 changes: 6 additions & 6 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ func Log(z *big.Float) *big.Float {

// Log(0) = -Inf
if z.Sign() == 0 {
return big.NewFloat(math.Inf(-1))
return big.NewFloat(math.Inf(-1)).SetPrec(z.Prec())
}

prec := z.Prec() + 64 // guard digits

one := new(big.Float).SetPrec(prec).SetInt64(1)
two := new(big.Float).SetPrec(prec).SetInt64(2)
four := new(big.Float).SetPrec(prec).SetInt64(4)
one := big.NewFloat(1).SetPrec(prec)
two := big.NewFloat(2).SetPrec(prec)
four := big.NewFloat(4).SetPrec(prec)

// Log(1) = 0
if z.Cmp(one) == 0 {
return new(big.Float).SetPrec(z.Prec()).SetInt64(0)
return big.NewFloat(0).SetPrec(z.Prec())
}

// Log(+Inf) = +Inf
Expand All @@ -38,7 +38,7 @@ func Log(z *big.Float) *big.Float {

x := new(big.Float).SetPrec(prec)

// if 0 < x < 1 we compute log(x) as -log(1/x)
// if 0 < z < 1 we compute log(z) as -log(1/z)
var neg bool
if z.Cmp(one) < 0 {
x.Quo(one, z)
Expand Down
10 changes: 5 additions & 5 deletions pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ func Pow(z *big.Float, w *big.Float) *big.Float {
// Pow(z, 1) = z
// Pow(+Inf, n) = +Inf
if w.Cmp(big.NewFloat(1)) == 0 || z.IsInf() {
x := new(big.Float)
return x.Copy(z)
return new(big.Float).Copy(z)
}

// Pow(z, -w) = 1 / Pow(z, w)
if w.Sign() < 0 {
x := new(big.Float)
zExt := new(big.Float).Copy(z).SetPrec(z.Prec() + 64)
wNeg := new(big.Float).Copy(w).Neg(w)
wNeg := new(big.Float).Neg(w)
return x.Quo(big.NewFloat(1), Pow(zExt, wNeg)).SetPrec(z.Prec())
}

Expand All @@ -36,6 +35,7 @@ func Pow(z *big.Float, w *big.Float) *big.Float {
return powInt(z, int(wi))
}

// compute w**z as exp(z log(w))
x := new(big.Float).SetPrec(z.Prec() + 64)
logZ := Log(new(big.Float).Copy(z).SetPrec(z.Prec() + 64))
x.Mul(w, logZ)
Expand All @@ -55,7 +55,7 @@ func powInt(z *big.Float, w int) *big.Float {
exp = exp * w

// result's mantissa
x := new(big.Float).SetPrec(z.Prec()).SetFloat64(1.0)
x := big.NewFloat(1).SetPrec(z.Prec())

// Classic right-to-left binary exponentiation
for w > 0 {
Expand All @@ -66,5 +66,5 @@ func powInt(z *big.Float, w int) *big.Float {
mant.Mul(mant, mant)
}

return new(big.Float).SetMantExp(x, exp).SetPrec(z.Prec())
return new(big.Float).SetMantExp(x, exp)
}
14 changes: 6 additions & 8 deletions sqrt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func Sqrt(z *big.Float) *big.Float {

// Sqrt(±0) = ±0
if z.Sign() == 0 {
return big.NewFloat(float64(z.Sign()))
return big.NewFloat(0).SetPrec(z.Prec())
}

// Sqrt(+Inf) = +Inf
if z.IsInf() {
return big.NewFloat(math.Inf(+1))
return big.NewFloat(math.Inf(+1)).SetPrec(z.Prec())
}

prec := z.Prec() + 64 // guard digits
Expand All @@ -46,7 +46,7 @@ func Sqrt(z *big.Float) *big.Float {
if zfs := math.Sqrt(zf); zfs != 0 && 1/zfs != 0 {
x.SetFloat64(1 / zfs)
} else {
return sqrt_big(z)
return sqrtBig(z)
}

// we need at least log_2(prec) iterations
Expand All @@ -70,20 +70,18 @@ func Sqrt(z *big.Float) *big.Float {
return x.SetPrec(z.Prec())
}

func sqrt_big(z *big.Float) *big.Float {
func sqrtBig(z *big.Float) *big.Float {

prec := z.Prec() + 64

one := new(big.Float).SetPrec(prec).SetInt64(1)
half := new(big.Float).SetPrec(prec).SetFloat64(0.5)
one := big.NewFloat(1).SetPrec(prec)
half := big.NewFloat(0.5)

x := new(big.Float).SetPrec(prec).SetMantExp(one, z.MantExp(nil)/2)

t := new(big.Float)

// Classic Newton iteration:
// x_{n+1} = 1/2 * ( x_n + (S / x_n) )

steps := int(math.Log2(float64(prec))) + 1
for i := 0; i < steps; i++ {
t.Quo(z, x) // t = S / x_n
Expand Down

0 comments on commit 27fd314

Please sign in to comment.