本文整理汇总了Golang中math/big.Float.Neg方法的典型用法代码示例。如果您正苦于以下问题:Golang Float.Neg方法的具体用法?Golang Float.Neg怎么用?Golang Float.Neg使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Float
的用法示例。
在下文中一共展示了Float.Neg方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: quo
func quo(x, y *complexFloat) *complexFloat {
z := newComplexFloat()
denominator := new(big.Float).SetPrec(prec)
c2 := new(big.Float).SetPrec(prec)
d2 := new(big.Float).SetPrec(prec)
c2.Mul(y.r, y.r)
d2.Mul(y.i, y.i)
denominator.Add(c2, d2)
if denominator.Cmp(zero) == 0 || denominator.IsInf() {
return newComplexFloat()
}
ac := new(big.Float).SetPrec(prec)
bd := new(big.Float).SetPrec(prec)
ac.Mul(x.r, y.r)
bd.Mul(x.i, y.i)
bc := new(big.Float).SetPrec(prec)
ad := new(big.Float).SetPrec(prec)
bc.Mul(x.i, y.r)
ad.Mul(x.r, y.i)
z.r.Add(ac, bd)
z.r.Quo(z.r, denominator)
z.i.Add(bc, ad.Neg(ad))
z.i.Quo(z.i, denominator)
return z
}
示例2: sincos
// sincos iterates a sin or cos Taylor series.
func sincos(name string, c Context, index int, x *big.Float, z *big.Float, exp uint64, factorial *big.Float) *big.Float {
term := newFloat(c).Set(floatOne)
for j := 0; j < index; j++ {
term.Mul(term, x)
}
xN := newFloat(c).Set(term)
x2 := newFloat(c).Mul(x, x)
n := newFloat(c)
for loop := newLoop(c.Config(), name, x, 4); ; {
// Invariant: factorial holds -1ⁿ*exponent!.
factorial.Neg(factorial)
term.Quo(term, factorial)
z.Add(z, term)
if loop.done(z) {
break
}
// Advance x**index (multiply by x²).
term.Mul(xN, x2)
xN.Set(term)
// Advance factorial.
factorial.Mul(factorial, n.SetUint64(exp+1))
factorial.Mul(factorial, n.SetUint64(exp+2))
exp += 2
}
return z
}
示例3: mul
func mul(x, y *complexFloat) *complexFloat {
t1 := new(big.Float).SetPrec(prec)
t2 := new(big.Float).SetPrec(prec)
z := newComplexFloat()
t1.Mul(x.r, y.r)
t2.Mul(x.i, y.i)
t2.Neg(t2)
z.r.Add(t1, t2)
t1.Mul(x.r, y.i)
t2.Mul(x.i, y.r)
z.i.Add(t1, t2)
return z
}
示例4: floatSin
// floatSin computes sin(x) using argument reduction and a Taylor series.
func floatSin(c Context, x *big.Float) *big.Float {
negate := false
if x.Sign() < 0 {
x.Neg(x)
negate = true
}
twoPiReduce(c, x)
// sin(x) = x - x³/3! + x⁵/5! - ...
// First term to compute in loop will be -x³/3!
factorial := newFloat(c).SetInt64(6)
result := sincos("sin", c, 3, x, newFloat(c).Set(x), 3, factorial)
if negate {
result.Neg(result)
}
return result
}
示例5: Log
// 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 =
// +Inf
func Log(z *big.Float) *big.Float {
// panic on negative z
if z.Sign() == -1 {
panic("Log: argument is negative")
}
// Log(0) = -Inf
if z.Sign() == 0 {
return big.NewFloat(math.Inf(-1)).SetPrec(z.Prec())
}
prec := z.Prec() + 64 // guard digits
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 big.NewFloat(0).SetPrec(z.Prec())
}
// Log(+Inf) = +Inf
if z.IsInf() {
return big.NewFloat(math.Inf(+1)).SetPrec(z.Prec())
}
x := new(big.Float).SetPrec(prec)
// if 0 < z < 1 we compute log(z) as -log(1/z)
var neg bool
if z.Cmp(one) < 0 {
x.Quo(one, z)
neg = true
} else {
x.Set(z)
}
// We scale up x until x >= 2**(prec/2), and then we'll be allowed
// to use the AGM formula for Log(x).
//
// Double x until the condition is met, and keep track of the
// number of doubling we did (needed to scale back later).
lim := new(big.Float)
lim.SetMantExp(two, int(prec/2))
k := 0
for x.Cmp(lim) < 0 {
x.Mul(x, x)
k++
}
// Compute the natural log of x using the fact that
// log(x) = π / (2 * AGM(1, 4/x))
// if
// x >= 2**(prec/2),
// where prec is the desired precision (in bits)
pi := pi(prec)
agm := agm(one, x.Quo(four, x)) // agm = AGM(1, 4/x)
x.Quo(pi, x.Mul(two, agm)) // reuse x, we don't need it
if neg {
x.Neg(x)
}
// scale the result back multiplying by 2**-k
// reuse lim to reduce allocations.
x.Mul(x, lim.SetMantExp(one, -k))
return x.SetPrec(z.Prec())
}
示例6: String
func (f BigFloat) String() string {
var mant big.Float
exp := f.Float.MantExp(&mant)
positive := 1
if exp < 0 {
positive = 0
exp = -exp
}
verb, prec := byte('g'), 12
format := conf.Format()
if format != "" {
v, p, ok := conf.FloatFormat()
if ok {
verb, prec = v, p
}
}
// Printing huge floats can be very slow using
// big.Float's native methods; see issue #11068.
// For example 1e5000000 takes a minute of CPU time just
// to print. The code below is instantaneous, by rescaling
// first. It is however less feature-complete.
// (Big ints are problematic too, but if you print 1e50000000
// as an integer you probably won't be surprised it's slow.)
if fastFloatPrint && exp > 10000 {
// We always use %g to print the fraction, and it will
// never have an exponent, but if the format is %E we
// need to use a capital E.
eChar := 'e'
if verb == 'E' || verb == 'G' {
eChar = 'E'
}
fexp := newF().SetInt64(int64(exp))
fexp.Mul(fexp, floatLog2)
fexp.Quo(fexp, floatLog10)
// We now have a floating-point base 10 exponent.
// Break into the integer part and the fractional part.
// The integer part is what we will show.
// The 10**(fractional part) will be multiplied back in.
iexp, _ := fexp.Int(nil)
fraction := fexp.Sub(fexp, newF().SetInt(iexp))
// Now compute 10**(fractional part).
// Fraction is in base 10. Move it to base e.
fraction.Mul(fraction, floatLog10)
scale := exponential(fraction)
if positive > 0 {
mant.Mul(&mant, scale)
} else {
mant.Quo(&mant, scale)
}
ten := newF().SetInt64(10)
i64exp := iexp.Int64()
// For numbers not too far from one, print without the E notation.
// Shouldn't happen (exp must be large to get here) but just
// in case, we keep this around.
if -4 <= i64exp && i64exp <= 11 {
if i64exp > 0 {
for i := 0; i < int(i64exp); i++ {
mant.Mul(&mant, ten)
}
} else {
for i := 0; i < int(-i64exp); i++ {
mant.Quo(&mant, ten)
}
}
return fmt.Sprintf("%g\n", &mant)
} else {
sign := ""
if mant.Sign() < 0 {
sign = "-"
mant.Neg(&mant)
}
// If it has a leading zero, rescale.
digits := mant.Text('g', prec)
for digits[0] == '0' {
mant.Mul(&mant, ten)
if positive > 0 {
i64exp--
} else {
i64exp++
}
digits = mant.Text('g', prec)
}
return fmt.Sprintf("%s%s%c%c%d", sign, digits, eChar, "-+"[positive], i64exp)
}
}
return f.Float.Text(verb, prec)
}
示例7: Run
//.........这里部分代码省略.........
// For i := 1 to n:
// for j := i + 1 to n − 1:
// set Hij := 0
// endfor
// if i ≤ n − 1 then set Hii := s_(i+1)/s_i
// for j := 1 to i−1:
// set Hij := −y_i * y_j / (s_j * s_(j+1))
// endfor
// endfor
for i := 1; i <= n; i++ {
for j := i + 1; j < n; j++ {
H[i][j].SetInt64(0)
}
if i <= n-1 {
if s[i].Sign() == 0 {
// Precision probably exhausted
return nil, ErrorPrecisionExhausted
}
// H[i][i] = (s[i+1] << prec) / s[i]
H[i][i].Quo(&s[i+1], &s[i])
}
for j := 1; j < i; j++ {
var sjj1 big.Float
sjj1.Mul(&s[j], &s[j+1])
if debug {
fmt.Printf("sjj1 = %f\n", &sjj1)
}
if sjj1.Sign() == 0 {
// Precision probably exhausted
return nil, ErrorPrecisionExhausted
}
// H[i][j] = ((-y[i] * y[j]) << prec) / sjj1
tmp0.Mul(&y[i], &y[j])
tmp0.Neg(tmp0)
H[i][j].Quo(tmp0, &sjj1)
}
}
if debug {
fmt.Println("Init Step 3")
printMatrix("H", H)
}
// Init Step 4
//
// Perform full reduction on H, simultaneously updating y, A and B:
//
// For i := 2 to n:
// for j := i−1 to 1 step−1:
// t := nint(Hij/Hjj)
// y_j := y_j + t * y_i
// for k := 1 to j:
// Hik := Hik − t * Hjk
// endfor
// for k := 1 to n:
// Aik := Aik − t * Ajk
// Bkj := Bkj + t * Bki
// endfor
// endfor
// endfor
for i := 2; i <= n; i++ {
for j := i - 1; j > 0; j-- {
//t = floor(H[i][j]/H[j,j] + 0.5)
var t big.Int
var tFloat big.Float
if H[j][j].Sign() == 0 {
// Precision probably exhausted
return nil, ErrorPrecisionExhausted