本文整理汇总了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 --")
}
示例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")
}
}
示例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)
}
示例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
}
示例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
}
示例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")
}
示例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))
})
}
示例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
}
示例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))
}
示例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)
}
示例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)
}
示例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)
}
示例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))
})
}
示例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)
}
示例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
}