本文整理汇总了Golang中math/big.Rat类的典型用法代码示例。如果您正苦于以下问题:Golang Rat类的具体用法?Golang Rat怎么用?Golang Rat使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Rat类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Total
// Total computes the Usage.Total() of all the Usages of a Basket
func (b *Basket) Total() *big.Rat {
total := new(big.Rat)
for _, usage := range *b {
total = total.Add(total, usage.Total())
}
return total
}
示例2: TestAmountJSON
func TestAmountJSON(t *testing.T) {
type item struct {
Price *Amount
}
const expected = `{"Price":"367/100"}`
p := new(big.Rat)
p.SetString("367/100")
x := &item{Price: (*Amount)(p)}
s, err := json.Marshal(x)
if err != nil {
t.Fatal(err)
}
if string(s) != expected {
t.Errorf("got %q, expected %q", string(s), expected)
}
x = new(item)
err = json.Unmarshal([]byte(expected), x)
if err != nil {
t.Fatal(err)
}
if str := x.Price.String(); str != "3.67" {
t.Errorf("got %s, expected %s", str, "3.67")
}
}
示例3: extr
func (t *lft) extr(x *big.Int) *big.Rat {
var n, d big.Int
var r big.Rat
return r.SetFrac(
n.Add(n.Mul(&t.q, x), &t.r),
d.Add(d.Mul(&t.s, x), &t.t))
}
示例4: Total
// Total returns the total of an Usage
func (u *Usage) Total() *big.Rat {
//return math.Min(u.Object.UnitPrice * u.BillableQuantity(), u.Object.UnitPriceCap)
total := new(big.Rat).Mul(u.BillableQuantity(), u.Object.UnitPrice)
total = total.Quo(total, u.Object.UnitQuantity)
return ratMin(total, u.Object.UnitPriceCap)
}
示例5: BigratToBigint
// BigratToInt converts a *big.Rat to a *big.Int (with truncation)
func BigratToBigint(bigrat *big.Rat) *big.Int {
int_string := bigrat.FloatString(0)
bigint := new(big.Int)
// no error scenario could be imagined in testing, so discard err
fmt.Sscan(int_string, bigint)
return bigint
}
示例6: PutString
func (me *StatisticalAccumulator) PutString(x string) {
xx := new(big.Rat)
xx.SetString(x)
me.PutRat(xx)
}
示例7: 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)
}
示例8: PutInt64
func (me *StatisticalAccumulator) PutInt64(x int64) {
xx := new(big.Rat)
xx.SetInt64(x)
me.PutRat(xx)
}
示例9: PutFrac64
func (me *StatisticalAccumulator) PutFrac64(a, b int64) {
xx := new(big.Rat)
xx.SetFrac64(a, b)
me.PutRat(xx)
}
示例10: PutFrac
func (me *StatisticalAccumulator) PutFrac(a, b *big.Int) {
xx := new(big.Rat)
xx.SetFrac(a, b)
me.PutRat(xx)
}
示例11: PrintRegister
// Prints each transaction that matches the given filters.
func PrintRegister(generalLedger []*ledger.Transaction, filterArr []string, columns int) {
runningBalance := new(big.Rat)
for _, trans := range generalLedger {
for _, accChange := range trans.AccountChanges {
inFilter := len(filterArr) == 0
for _, filter := range filterArr {
if strings.Contains(accChange.Name, filter) {
inFilter = true
}
}
if inFilter {
runningBalance.Add(runningBalance, accChange.Balance)
writtenBytes, _ := fmt.Printf("%s %s", trans.Date.Format(ledger.TransactionDateFormat), trans.Payee)
outBalanceString := accChange.Balance.FloatString(ledger.DisplayPrecision)
outRunningBalanceString := runningBalance.FloatString(ledger.DisplayPrecision)
spaceCount := columns - writtenBytes - 2 - len(outBalanceString) - len(outRunningBalanceString)
if spaceCount < 0 {
spaceCount = 0
}
fmt.Printf("%s%s %s", strings.Repeat(" ", spaceCount), outBalanceString, outRunningBalanceString)
fmt.Println("")
}
}
}
}
示例12: walletbalancecmd
// walletbalancecmd retrieves and displays information about the wallet.
func walletbalancecmd() {
status := new(api.WalletGET)
err := getAPI("/wallet", status)
if err != nil {
die("Could not get wallet status:", err)
}
encStatus := "Unencrypted"
if status.Encrypted {
encStatus = "Encrypted"
}
lockStatus := "Locked"
balance := "Unlock the wallet to view balance"
if status.Unlocked {
lockStatus = "Unlocked"
// Divide by 1e24 to get SC.
r := new(big.Rat).SetFrac(status.ConfirmedSiacoinBalance.Big(), new(big.Int).Exp(big.NewInt(10), big.NewInt(24), nil))
sc, _ := r.Float64()
unconfirmedBalance := status.ConfirmedSiacoinBalance.Add(status.UnconfirmedIncomingSiacoins).Sub(status.UnconfirmedOutgoingSiacoins)
unconfirmedDifference := new(big.Int).Sub(unconfirmedBalance.Big(), status.ConfirmedSiacoinBalance.Big())
r = new(big.Rat).SetFrac(unconfirmedDifference, new(big.Int).Exp(big.NewInt(10), big.NewInt(24), nil))
usc, _ := r.Float64()
balance = fmt.Sprintf(`Confirmed Balance: %.2f SC
Unconfirmed Delta: %+.2f SC
Exact: %v H
Siafunds: %v SF
Siafund Claims: %v H
`, sc, usc, status.ConfirmedSiacoinBalance, status.SiafundBalance, status.SiacoinClaimBalance)
}
fmt.Printf(`Wallet status:
%s, %s
%s
`, encStatus, lockStatus, balance)
}
示例13: EstimatePiFromPrevious
func EstimatePiFromPrevious(iteration, previousIndex int, sum *big.Rat) (*big.Rat, *big.Rat) {
for i := previousIndex; i < iteration; i++ {
sum.Add(sum, sn(i))
}
pi := getPiFromSum(sum)
return pi, sum
}
示例14: walletstatuscmd
// walletstatuscmd retrieves and displays information about the wallet
func walletstatuscmd() {
status := new(api.WalletGET)
err := getAPI("/wallet", status)
if err != nil {
fmt.Println("Could not get wallet status:", err)
return
}
encStatus := "Unencrypted"
if status.Encrypted {
encStatus = "Encrypted"
}
lockStatus := "Locked"
if status.Unlocked {
lockStatus = "Unlocked"
}
// divide by 1e24 to get SC
r := new(big.Rat).SetFrac(status.ConfirmedSiacoinBalance.Big(), new(big.Int).Exp(big.NewInt(10), big.NewInt(24), nil))
sc, _ := r.Float64()
unconfirmedBalance := status.ConfirmedSiacoinBalance.Add(status.UnconfirmedIncomingSiacoins)
unconfirmedBalance = unconfirmedBalance.Sub(status.UnconfirmedOutgoingSiacoins)
r = new(big.Rat).SetFrac(unconfirmedBalance.Big(), new(big.Int).Exp(big.NewInt(10), big.NewInt(24), nil))
usc, _ := r.Float64()
fmt.Printf(`Wallet status:
%s, %s
Confirmed Balance: %.2f SC
Unconfirmed Balance: %.2f SC
Exact: %v H
`, encStatus, lockStatus, sc, usc, status.ConfirmedSiacoinBalance)
}
示例15: Rat
// Rat returns the rational number representation of z.
// If x is non-nil, Rat stores the result in x instead of
// allocating a new Rat.
func (z *Decimal) Rat(x *big.Rat) *big.Rat {
// TODO(eric):
// We use big.Ints here when technically we could use our
// int64 with big.Rat's SetInt64 methoz.
// I'm not sure if it'll be an optimization or not.
var num, denom big.Int
if z.compact != overflown {
c := big.NewInt(z.compact)
if z.scale >= 0 {
num.Set(c)
denom.Set(mulBigPow10(oneInt, z.scale))
} else {
num.Set(mulBigPow10(c, -z.scale))
denom.SetInt64(1)
}
} else {
if z.scale >= 0 {
num.Set(&z.mantissa)
denom.Set(mulBigPow10(oneInt, z.scale))
} else {
num.Set(mulBigPow10(&z.mantissa, -z.scale))
denom.SetInt64(1)
}
}
if x != nil {
return x.SetFrac(&num, &denom)
}
return new(big.Rat).SetFrac(&num, &denom)
}