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


Golang big.NewRat函数代码示例

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


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

示例1: TestFormatMoney

func TestFormatMoney(t *testing.T) {
	accounting := Accounting{Symbol: "$", Precision: 2}
	AssertEqual(t, accounting.FormatMoney(123456789.213123), "$123,456,789.21")
	AssertEqual(t, accounting.FormatMoney(12345678), "$12,345,678.00")
	AssertEqual(t, accounting.FormatMoney(-12345678), "$-12,345,678.00")
	AssertEqual(t, accounting.FormatMoney(0), "$0.00")
	AssertEqual(t, accounting.FormatMoney(big.NewRat(77777777, 3)), "$25,925,925.67")
	AssertEqual(t, accounting.FormatMoney(big.NewRat(-77777777, 3)), "$-25,925,925.67")

	accounting = Accounting{Symbol: "$", Precision: 0, Format: "%s %v"}
	AssertEqual(t, accounting.FormatMoney(123456789.213123), "$ 123,456,789")
	AssertEqual(t, accounting.FormatMoney(12345678), "$ 12,345,678")
	AssertEqual(t, accounting.FormatMoney(-12345678), "$ -12,345,678")
	AssertEqual(t, accounting.FormatMoney(0), "$ 0")

	accounting = Accounting{Symbol: "€", Precision: 2, Thousand: ".", Decimal: ","}
	AssertEqual(t, accounting.FormatMoney(4999.99), "€4.999,99")
	AssertEqual(t, accounting.FormatMoney(-4999.99), "€-4.999,99")

	accounting = Accounting{Symbol: "£ ", Precision: 0}
	AssertEqual(t, accounting.FormatMoney(-500000), "£ -500,000")

	accounting = Accounting{Symbol: "GBP", Precision: 0,
		Format: "%s %v", FormatNegative: "%s (%v)", FormatZero: "%s --"}
	AssertEqual(t, accounting.FormatMoney(1000000), "GBP 1,000,000")
	AssertEqual(t, accounting.FormatMoney(-5000), "GBP (5,000)")
	AssertEqual(t, accounting.FormatMoney(0), "GBP --")
}
开发者ID:postfix,项目名称:accounting,代码行数:28,代码来源:accounting_test.go

示例2: TestTargetMul

// TestTargetMul probes the Mul function of the target type.
func TestTargetMul(t *testing.T) {
	var target2, target6, target10, target14, target20 Target
	target2[crypto.HashSize-1] = 2
	target6[crypto.HashSize-1] = 6
	target10[crypto.HashSize-1] = 10
	target14[crypto.HashSize-1] = 14
	target20[crypto.HashSize-1] = 20

	// Multiplying the difficulty of a target at '10' by 5 will yeild a target
	// of '2'. Similar math follows for the remaining checks.
	expect2 := target10.MulDifficulty(big.NewRat(5, 1))
	if expect2 != target2 {
		t.Error(expect2)
		t.Error(target2)
		t.Error("Target.Mul did not work as expected")
	}
	expect6 := target10.MulDifficulty(big.NewRat(3, 2))
	if expect6 != target6 {
		t.Error("Target.Mul did not work as expected")
	}
	expect14 := target10.MulDifficulty(big.NewRat(7, 10))
	if expect14 != target14 {
		t.Error("Target.Mul did not work as expected")
	}
	expect20 := target10.MulDifficulty(big.NewRat(1, 2))
	if expect20 != target20 {
		t.Error("Target.Mul did not work as expected")
	}
}
开发者ID:mm3,项目名称:Sia,代码行数:30,代码来源:target_test.go

示例3: SubmitHashrate

func (self *Server) SubmitHashrate(mr *Miner) error {
	claim := mr.getClaimedHashrate()
	actual := mr.getTrueHashrate()

	uiHashrate := big.NewRat(1, 1)
	uiHashrate.SetFrac(claim, big.NewInt(1))

	upperBound := big.NewRat(2, 1)
	lowerBound := big.NewRat(1, 2)

	actualRat := big.NewRat(1, 1)
	actualRat.SetFrac(actual, big.NewInt(1))
	upperBound.Mul(upperBound, actualRat)
	lowerBound.Mul(lowerBound, actualRat)

	if uiHashrate.Cmp(upperBound) > 0 {
		uiHashrate.Set(upperBound)
	}

	if uiHashrate.Cmp(lowerBound) < 0 {
		uiHashrate.Set(lowerBound)
	}

	fhashrate, _ := uiHashrate.Float64()

	msg := get_jsonHashrate(mr.address, big.NewInt(int64(fhashrate)))

	self.SendMessage("addHashes", msg)

	return error(nil)
}
开发者ID:OneEther,项目名称:OneBackend,代码行数:31,代码来源:server.go

示例4: computeMul

func computeMul(lst []int) *big.Rat {
	res := big.NewRat(1, 1)
	for _, v := range lst {
		res.Mul(res, big.NewRat(int64(v), 1))
	}
	return res
}
开发者ID:bonnefoa,项目名称:gobot,代码行数:7,代码来源:metapi.go

示例5: getAccountShare

// for PPLNS, not used
func getAccountShare(divvy, hashes, totalHashes *big.Int) *big.Int {
	zero := big.NewInt(0)

	rat := big.NewRat(1.0, 1.0)
	divvyRat := big.NewRat(1.0, 1.0)
	share := big.NewRat(1.0, 1.0)
	intShare := big.NewInt(0)
	cut := big.NewRat(1.0, 1.0)
	cut.SetFloat64(1.0 - HOUSE_RAKE)

	if hashes.Cmp(zero) == 0 {
		return zero
	}

	if totalHashes.Cmp(big.NewInt(0)) >= 0 {
		rat.SetFrac(hashes, totalHashes)
		rat.Mul(rat, cut)
		divvyRat.SetInt(divvy)
		share.Mul(rat, divvyRat)

		intShare.Set(share.Num())
		intShare.Div(intShare, share.Denom())
	}

	return intShare
}
开发者ID:OneEther,项目名称:OneBackend,代码行数:27,代码来源:server.go

示例6: main

func main() {
	const (
		xmin, ymin, xmax, ymax = -2, -2, +2, +2
		width, height          = 1024, 1024
	)

	img := image.NewRGBA(image.Rect(0, 0, width, height))
	for py := 0; py < height; py++ {
		y := float64(py)/height*(ymax-ymin) + ymin
		for px := 0; px < width; px++ {
			x := float64(px)/width*(xmax-xmin) + xmin
			//			z := complex(x, y)
			// Image point (px, py) represents complex value z.
			//			img.Set(px, py, mandelbrot128(z))
			k := mandelbrotRat(big.NewRat(0, 1).SetFloat64(x), big.NewRat(0, 1).SetFloat64(y))
			fmt.Printf("%v,%v,%v\n", py, px, k)
			img.Set(px, py, k)
		}
	}
	fmt.Printf("Create\n")
	out, _ := os.OpenFile("out8.png", os.O_WRONLY|os.O_CREATE, 0600)
	defer out.Close()
	png.Encode(out, img)
	fmt.Printf("Done\n")
}
开发者ID:zDpxq6,项目名称:go,代码行数:25,代码来源:main.go

示例7: TestNewUsageByPathWithQuantity

func TestNewUsageByPathWithQuantity(t *testing.T) {
	Convey("Testing NewUsageByPathWithQuantity()", t, func() {
		usage := NewUsageByPathWithQuantity("/compute/c1/run", big.NewRat(1, 1))
		So(usage.PricingObject.Path, ShouldEqual, "/compute/c1/run")
		So(usage.Quantity, ShouldEqualBigRat, big.NewRat(1, 1))
	})
}
开发者ID:arianvp,项目名称:scaleway-cli,代码行数:7,代码来源:usage_test.go

示例8: calcBalances

func calcBalances(calcAccts []calculatedAccount, balances []*ledger.Account) (results []*ledger.Account) {
	accVals := make(map[string]*big.Rat)
	for _, calcAccount := range calcAccts {
		for _, bal := range balances {
			for _, acctOp := range calcAccount.AccountOperations {
				if acctOp.Name == bal.Name {
					fval := big.NewRat(1, 1).Abs(bal.Balance)
					aval, found := accVals[calcAccount.Name]
					if !found {
						aval = big.NewRat(0, 1)
					}
					switch acctOp.Operation {
					case "+":
						aval.Add(aval, fval)
					case "-":
						aval.Sub(aval, fval)
					}
					accVals[calcAccount.Name] = aval
				}
			}
		}
	}

	for _, calcAccount := range calcAccts {
		results = append(results, &ledger.Account{Name: calcAccount.Name, Balance: accVals[calcAccount.Name]})
	}

	return
}
开发者ID:howeyc,项目名称:ledger,代码行数:29,代码来源:handler_report.go

示例9: targetAdjustmentBase

// targetAdjustmentBase returns the magnitude that the target should be
// adjusted by before a clamp is applied.
func (bn *blockNode) targetAdjustmentBase() *big.Rat {
	// Target only adjusts twice per window.
	if bn.height%(types.TargetWindow/2) != 0 {
		return big.NewRat(1, 1)
	}

	// Grab the block that was generated 'TargetWindow' blocks prior to the
	// parent. If there are not 'TargetWindow' blocks yet, stop at the genesis
	// block.
	var windowSize types.BlockHeight
	windowStart := bn
	for windowSize = 0; windowSize < types.TargetWindow && windowStart.parent != nil; windowSize++ {
		windowStart = windowStart.parent
	}

	// The target of a child is determined by the amount of time that has
	// passed between the generation of its immediate parent and its
	// TargetWindow'th parent. The expected amount of seconds to have passed is
	// TargetWindow*BlockFrequency. The target is adjusted in proportion to how
	// time has passed vs. the expected amount of time to have passed.
	//
	// The target is converted to a big.Rat to provide infinite precision
	// during the calculation. The big.Rat is just the int representation of a
	// target.
	timePassed := bn.block.Timestamp - windowStart.block.Timestamp
	expectedTimePassed := types.BlockFrequency * windowSize
	return big.NewRat(int64(timePassed), int64(expectedTimePassed))
}
开发者ID:mm3,项目名称:Sia,代码行数:30,代码来源:blocknode.go

示例10: TestMaxMin

func TestMaxMin(t *testing.T) {
	a := big.NewRat(3, 1)
	minA := big.NewRat(1, 1)
	maxA := big.NewRat(10, 1)
	expected := big.NewRat(2, 9)
	result := MaxMin(maxA, minA, a)
	checkRats(expected, result, t)
}
开发者ID:kalkin,项目名称:btc,代码行数:8,代码来源:math_test.go

示例11: TestSub

func TestSub(t *testing.T) {
	x := big.NewRat(24, 1)
	y := big.NewRat(6, 1)
	z := big.NewRat(18, 1)
	result := Sub(x, y)
	checkRats(z, result, t)

}
开发者ID:kalkin,项目名称:btc,代码行数:8,代码来源:math_test.go

示例12: TestMul

func TestMul(t *testing.T) {
	x := big.NewRat(4, 1)
	y := big.NewRat(6, 1)
	z := big.NewRat(24, 1)
	result := Mul(x, y)
	checkRats(z, result, t)

}
开发者ID:kalkin,项目名称:btc,代码行数:8,代码来源:math_test.go

示例13: TestNewUsageWithQuantity

func TestNewUsageWithQuantity(t *testing.T) {
	Convey("Testing NewUsageWithQuantity()", t, func() {
		object := CurrentPricing.GetByPath("/compute/c1/run")
		usage := NewUsageWithQuantity(object, big.NewRat(1, 1))
		So(usage.Object.Path, ShouldEqual, "/compute/c1/run")
		So(usage.Quantity, ShouldEqualBigRat, big.NewRat(1, 1))
	})
}
开发者ID:awesome-docker,项目名称:scaleway-cli,代码行数:8,代码来源:usage_test.go

示例14: TestNormalizeSymbol

func TestNormalizeSymbol(t *testing.T) {
	symbolMax := big.NewRat(1, 1)
	symbolMin := big.NewRat(0, 1)
	symbol := big.NewRat(10, 100)
	expected := MaxMin(symbolMax, symbolMin, symbol)
	result := NormalizeSymbol(symbol)
	checkRats(expected, result, t)
}
开发者ID:kalkin,项目名称:btc,代码行数:8,代码来源:math_test.go

示例15: parseConditionalMultiplier

func parseConditionalMultiplier(s string) (*big.Rat, error) {
	rat := big.NewRat(0, 1)
	rat.SetString(s)
	if rat.Cmp(big.NewRat(1, 1)) > 0 {
		return rat, errors.New("conditional multiplier is larger than 1")
	}
	return rat, nil
}
开发者ID:jtremback,项目名称:upc-go,代码行数:8,代码来源:logic.go


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