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


Golang Rat.Inv方法代码示例

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


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

示例1: currencyUnits

// currencyUnits converts a types.Currency to a string with human-readable
// units. The unit used will be the largest unit that results in a value
// greater than 1. The value is rounded to 4 significant digits.
func currencyUnits(c types.Currency) string {
	pico := types.SiacoinPrecision.Div64(1e12)
	if c.Cmp(pico) < 0 {
		return c.String() + " H"
	}

	// iterate until we find a unit greater than c
	mag := pico
	unit := ""
	for _, unit = range []string{"pS", "nS", "uS", "mS", "SC", "KS", "MS", "GS", "TS"} {
		if c.Cmp(mag.Mul64(1e3)) < 0 {
			break
		} else if unit != "TS" {
			// don't want to perform this multiply on the last iter; that
			// would give us 1.235 TS instead of 1235 TS
			mag = mag.Mul64(1e3)
		}
	}

	num := new(big.Rat).SetInt(c.Big())
	denom := new(big.Rat).SetInt(mag.Big())
	res, _ := new(big.Rat).Mul(num, denom.Inv(denom)).Float64()

	return fmt.Sprintf("%.4g %s", res, unit)
}
开发者ID:robvanmieghem,项目名称:Sia,代码行数:28,代码来源:parse.go

示例2: Mean

func (me *StatisticalAccumulator) Mean() *big.Rat {
	mean := new(big.Rat)

	mean.Inv(me.n)

	mean.Mul(mean, me.sigmaXI)

	return mean
}
开发者ID:reiver,项目名称:go-statisticalaccumulator,代码行数:9,代码来源:statisticalaccumulator.go

示例3: ratScale

// ratScale multiplies x by 10**exp.
func ratScale(x *big.Rat, exp int) {
	if exp < 0 {
		x.Inv(x)
		ratScale(x, -exp)
		x.Inv(x)
		return
	}
	for exp >= 9 {
		x.Quo(x, bigRatBillion)
		exp -= 9
	}
	for exp >= 1 {
		x.Quo(x, bigRatTen)
		exp--
	}
}
开发者ID:zzn01,项目名称:ivy,代码行数:17,代码来源:bigrat.go

示例4: BirthdayProblem

// Returns an approximate Birthday probability calculation
// based on the number of blocks given and the hash size.
//
// It uses the simplified calculation:  p = k(k-1) / (2N)
//
// From http://preshing.com/20110504/hash-collision-probabilities/
func BirthdayProblem(blocks int) string {
	k := big.NewInt(int64(blocks))
	km1 := big.NewInt(int64(blocks - 1))
	ksq := k.Mul(k, km1)
	n := big.NewInt(0)
	n = n.Exp(big.NewInt(2), big.NewInt(int64(HashSize)*8), nil)
	twoN := n.Add(n, n)
	var t, t2 big.Rat
	var res *big.Rat
	//
	res = t.SetFrac(ksq, twoN)
	f64, _ := res.Float64()
	inv := t2.Inv(res).FloatString(0)
	invs := fmt.Sprintf(" ~ 1/%s ~ %v", inv, f64)

	return "Collision probability is" + invs
}
开发者ID:roger2000hk,项目名称:dedup,代码行数:23,代码来源:dedup.go

示例5: Variance

func (me *StatisticalAccumulator) Variance() *big.Rat {
	variance := new(big.Rat)

	variance.Inv(me.n)

	variance.Mul(variance, me.sigmaXISquared)

	temp := new(big.Rat)

	temp.Mul(me.n, me.n)
	temp.Inv(temp)

	temp.Mul(temp, me.sigmaXI)
	temp.Mul(temp, me.sigmaXI)

	variance.Sub(variance, temp)

	return variance
}
开发者ID:reiver,项目名称:go-statisticalaccumulator,代码行数:19,代码来源:statisticalaccumulator.go

示例6: ratExponent

// ratExponent returns the power of ten that x would display in scientific notation.
func ratExponent(x *big.Rat) int {
	if x.Sign() < 0 {
		x.Neg(x)
	}
	e := 0
	invert := false
	if x.Num().Cmp(x.Denom()) < 0 {
		invert = true
		x.Inv(x)
		e++
	}
	for x.Cmp(bigRatBillion) >= 0 {
		e += 9
		x.Quo(x, bigRatBillion)
	}
	for x.Cmp(bigRatTen) > 0 {
		e++
		x.Quo(x, bigRatTen)
	}
	if invert {
		return -e
	}
	return e
}
开发者ID:zzn01,项目名称:ivy,代码行数:25,代码来源:bigrat.go

示例7: problem57

// How many fractions contain a numerator with more digits than the denominator?
func problem57() int {
	sum := 0           // Number of fractions meeting the description.
	const limit = 1000 // Given in problem description.
	one := new(big.Rat).SetInt64(1)
	two := new(big.Rat).SetInt64(2)

	// result will be re-used each iteration to store the
	// current value of the fractional expansion.
	result := new(big.Rat)
	// tail will be re-used each iteration to store the
	// current value of the repeating component of the expansion.
	// That component is 2, (2 + 1/2), (2 + 1/(2 + 1/2)), ...
	tail := new(big.Rat).SetInt64(2)

	for i := 0; i < limit; i++ {
		temp := new(big.Rat)
		tail.Add(two, temp.Inv(tail))   // tail = (2 + 1/tail)
		result.Add(one, temp.Inv(tail)) // result = (1 + 1/tail)
		if checkNumerator(result) {
			sum++
		}
	}
	return sum
}
开发者ID:mjwestcott,项目名称:projecteuler,代码行数:25,代码来源:problem57.go

示例8: MulDifficulty

// Mul multiplies the difficulty of a target by y. The product is defined by:
//		y / x
func (x Target) MulDifficulty(y *big.Rat) (t Target) {
	product := new(big.Rat).Mul(y, x.Inverse())
	product = product.Inv(product)
	return RatToTarget(product)
}
开发者ID:CSSZiegler,项目名称:Sia,代码行数:7,代码来源:target.go

示例9: Div

// Wrapper for dividing two values
func Div(x, y *big.Rat) *big.Rat {
	yinv := y.Inv(y)
	z := x.Mul(x, yinv)

	return z
}
开发者ID:kalkin,项目名称:btc,代码行数:7,代码来源:math.go


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