当前位置: 首页>>代码示例>>Golang>>正文


Golang big.Float类代码示例

本文整理汇总了Golang中math/big.Float的典型用法代码示例。如果您正苦于以下问题:Golang Float类的具体用法?Golang Float怎么用?Golang Float使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Float类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: toRat

// toRat returns the fraction corresponding to x, or nil
// if x cannot be represented as a fraction a/b because
// its components a or b are too large.
func toRat(x *big.Float) *big.Rat {
	m := newFloat()
	e := x.MantExp(m)

	// fail to convert if fraction components are too large
	if e <= maxExp || e >= maxExp {
		return nil
	}

	// convert mantissa to big.Int value by shifting by ecorr
	ecorr := int(m.MinPrec())
	a, _ := m.SetMantExp(m, ecorr).Int(nil)
	e -= ecorr // correct exponent

	// compute actual fraction
	b := big.NewInt(1)
	switch {
	case e < 0:
		b.Lsh(b, uint(-e))
	case e > 0:
		a.Lsh(a, uint(e))
	}

	return new(big.Rat).SetFrac(a, b)
}
开发者ID:adityavs,项目名称:go,代码行数:28,代码来源:value.go

示例2: makeFloat

func makeFloat(x *big.Float) Value {
	// convert -0
	if x.Sign() == 0 {
		return floatVal0
	}
	return floatVal{x}
}
开发者ID:2thetop,项目名称:go,代码行数:7,代码来源:value.go

示例3: Sub_S

// Subtract
func (a Scalar) Sub_S(b S) S {
	var x, y big.Float
	x = big.Float(a)
	y = big.Float(b.(Scalar))
	z := x.Sub(&x, &y)
	return (Scalar)(*z)
}
开发者ID:grosenberg,项目名称:maths,代码行数:8,代码来源:vectorImpl.go

示例4: Div_S

// Divide
func (a Scalar) Div_S(b S) S {
	var x, y big.Float
	x = big.Float(a)
	y = big.Float(b.(Scalar))
	z := x.Quo(&x, &y)
	return (Scalar)(*z)
}
开发者ID:grosenberg,项目名称:maths,代码行数:8,代码来源:vectorImpl.go

示例5: String

func (m *meanagg) String() string {
	if m.d == 0 {
		return "NaN"
	}
	v := new(big.Float).Quo(m.v, big.NewFloat(m.d))
	return v.Text('f', -1)
}
开发者ID:robinjha,项目名称:clair,代码行数:7,代码来源:num.go

示例6: arithDivide

func arithDivide(a, b *big.Float) (*big.Float, error) {
	i, acc := b.Int64()
	if acc == big.Exact && i == 0 {
		return nil, fmt.Errorf("divide: by zero")
	}
	return new(big.Float).Quo(a, b), nil
}
开发者ID:tsandall,项目名称:opa,代码行数:7,代码来源:arithmetic.go

示例7: Encode

func (sed StringEncoderDecoder) Encode(w io.Writer, n *big.Float) error {
	// TODO - big.Float.MarshalText?
	// TODO - big.Float.Append
	str := []byte(n.Text('g', -1))
	_, err := w.Write(str)
	return err
}
开发者ID:Richardphp,项目名称:noms,代码行数:7,代码来源:string-encoder.go

示例8: Add_S

// Add
func (a Scalar) Add_S(b S) S {
	var x, y big.Float
	x = big.Float(a)
	y = big.Float(b.(Scalar))
	z := x.Add(&x, &y)
	return (Scalar)(*z)
}
开发者ID:grosenberg,项目名称:maths,代码行数:8,代码来源:vectorImpl.go

示例9: floatSqrt

// floatSqrt computes the square root of x using Newton's method.
// TODO: Use a better algorithm such as the one from math/sqrt.go.
func floatSqrt(c Context, x *big.Float) *big.Float {
	switch x.Sign() {
	case -1:
		Errorf("square root of negative number")
	case 0:
		return newFloat(c)
	}

	// Each iteration computes
	// 	z = z - (z²-x)/2z
	// z holds the result so far. A good starting point is to halve the exponent.
	// Experiments show we converge in only a handful of iterations.
	z := newFloat(c)
	exp := x.MantExp(z)
	z.SetMantExp(z, exp/2)

	// Intermediates, allocated once.
	zSquared := newFloat(c)
	num := newFloat(c)
	den := newFloat(c)

	for loop := newLoop(c.Config(), "sqrt", x, 1); ; {
		zSquared.Mul(z, z)
		num.Sub(zSquared, x)
		den.Mul(floatTwo, z)
		num.Quo(num, den)
		z.Sub(z, num)
		if loop.done(z) {
			break
		}
	}
	return z
}
开发者ID:db47h,项目名称:ivy,代码行数:35,代码来源:sqrt.go

示例10: smallRat

// smallRat reports whether x would lead to "reasonably"-sized fraction
// if converted to a *big.Rat.
func smallRat(x *big.Float) bool {
	if !x.IsInf() {
		e := x.MantExp(nil)
		return -maxExp < e && e < maxExp
	}
	return false
}
开发者ID:2thetop,项目名称:go,代码行数:9,代码来源:value.go

示例11: fconv

func fconv(fvp *Mpflt, flag FmtFlag) string {
	if flag&FmtSharp == 0 {
		return fvp.Val.Text('b', 0)
	}

	// use decimal format for error messages

	// determine sign
	f := &fvp.Val
	var sign string
	if f.Sign() < 0 {
		sign = "-"
		f = new(big.Float).Abs(f)
	} else if flag&FmtSign != 0 {
		sign = "+"
	}

	// Don't try to convert infinities (will not terminate).
	if f.IsInf() {
		return sign + "Inf"
	}

	// Use exact fmt formatting if in float64 range (common case):
	// proceed if f doesn't underflow to 0 or overflow to inf.
	if x, _ := f.Float64(); f.Sign() == 0 == (x == 0) && !math.IsInf(x, 0) {
		return fmt.Sprintf("%s%.6g", sign, x)
	}

	// Out of float64 range. Do approximate manual to decimal
	// conversion to avoid precise but possibly slow Float
	// formatting.
	// f = mant * 2**exp
	var mant big.Float
	exp := f.MantExp(&mant) // 0.5 <= mant < 1.0

	// approximate float64 mantissa m and decimal exponent d
	// f ~ m * 10**d
	m, _ := mant.Float64()                     // 0.5 <= m < 1.0
	d := float64(exp) * (math.Ln2 / math.Ln10) // log_10(2)

	// adjust m for truncated (integer) decimal exponent e
	e := int64(d)
	m *= math.Pow(10, d-float64(e))

	// ensure 1 <= m < 10
	switch {
	case m < 1-0.5e-6:
		// The %.6g format below rounds m to 5 digits after the
		// decimal point. Make sure that m*10 < 10 even after
		// rounding up: m*10 + 0.5e-5 < 10 => m < 1 - 0.5e6.
		m *= 10
		e--
	case m >= 10:
		m /= 10
		e++
	}

	return fmt.Sprintf("%s%.6ge%+d", sign, m, e)
}
开发者ID:achanda,项目名称:go,代码行数:59,代码来源:mpfloat.go

示例12: opsum

func opsum(a, b *big.Float) *big.Float {
	if a == nil {
		return b
	} else if b == nil {
		return a
	}
	return a.Add(a, b)
}
开发者ID:robinjha,项目名称:clair,代码行数:8,代码来源:num.go

示例13: Mul_S

// Multiply
func (a Scalar) Mul_S(b S) S {
	var x, y big.Float
	x = big.Float(a)
	y = big.Float(b.(Scalar))
	z := x.Mul(&x, &y)
	return (Scalar)(*z)

}
开发者ID:grosenberg,项目名称:maths,代码行数:9,代码来源:vectorImpl.go

示例14: Encode

func (bed BinaryEncoderDecoder) Encode(w io.Writer, n *big.Float) error {
	exponent := n.MantExp(bed.tmp)
	f, _ := bed.tmp.Float64()

	if err := binary.Write(w, binary.BigEndian, f); err != nil {
		return err
	}
	return binary.Write(w, binary.BigEndian, int32(exponent))
}
开发者ID:willhite,项目名称:noms-old,代码行数:9,代码来源:binary-encoder.go

示例15: pi

// Returns pi using Machin's formula
func pi(prec uint, result *big.Float) {
	var tmp, _4 big.Float
	_4.SetPrec(prec).SetInt64(4)
	acot(prec, 5, &tmp)
	tmp.SetPrec(prec).Mul(&tmp, &_4)
	acot(prec, 239, result)
	result.Sub(&tmp, result)
	result.SetPrec(prec).Mul(result, &_4)
}
开发者ID:ncw,项目名称:pslq,代码行数:10,代码来源:pslq_test.go


注:本文中的math/big.Float类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。