Skip to content

Commit

Permalink
prettify code
Browse files Browse the repository at this point in the history
  • Loading branch information
danil-lashin committed Jul 12, 2018
1 parent 5f29e36 commit db0d553
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 131 deletions.
2 changes: 2 additions & 0 deletions math/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"math/big"
)

// https://github.com/ALTree/bigfloat

// Exp returns a big.Float representation of exp(z). Precision is
// the same as the one of the argument. The function returns +Inf
// when z = +Inf, and 0 when z = -Inf.
Expand Down
99 changes: 0 additions & 99 deletions math/integer.go

This file was deleted.

2 changes: 2 additions & 0 deletions math/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"math/big"
)

// https://github.com/ALTree/bigfloat

// Log returns a big.Float representation of the natural logarithm of
// z. Precision is the same as the one of the argument. The function
// panics if z is negative, returns -Inf when z = 0, and +Inf when z =
Expand Down
2 changes: 2 additions & 0 deletions math/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package math

import "math/big"

// https://github.com/ALTree/bigfloat

// agm returns the arithmetic-geometric mean of a and b.
// a and b must have the same precision.
func agm(a, b *big.Float) *big.Float {
Expand Down
34 changes: 2 additions & 32 deletions math/pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package math

import "math/big"

// https://github.com/ALTree/bigfloat

// Pow returns a big.Float representation of z**w. Precision is the same as the one
// of the first argument. The function panics when z is negative.
func Pow(z *big.Float, w *big.Float) *big.Float {
Expand Down Expand Up @@ -29,13 +31,6 @@ func Pow(z *big.Float, w *big.Float) *big.Float {
return x.Quo(big.NewFloat(1), Pow(zExt, wNeg)).SetPrec(z.Prec())
}

// w integer fast path (disabled because introduces rounding
// errors)
if false && w.IsInt() {
wi, _ := w.Int64()
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))
Expand All @@ -44,28 +39,3 @@ func Pow(z *big.Float, w *big.Float) *big.Float {
return x.SetPrec(z.Prec())

}

// fast path for z**w when w is an integer
func powInt(z *big.Float, w int) *big.Float {

// get mantissa and exponent of z
mant := new(big.Float)
exp := z.MantExp(mant)

// result's exponent
exp = exp * w

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

// Classic right-to-left binary exponentiation
for w > 0 {
if w%2 == 1 {
x.Mul(x, mant)
}
w >>= 1
mant.Mul(mant, mant)
}

return new(big.Float).SetMantExp(x, exp)
}

0 comments on commit db0d553

Please sign in to comment.