本文整理汇总了Golang中math/big.Rat.Add方法的典型用法代码示例。如果您正苦于以下问题:Golang Rat.Add方法的具体用法?Golang Rat.Add怎么用?Golang Rat.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Rat
的用法示例。
在下文中一共展示了Rat.Add方法的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: 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
}
示例3: quo
func quo(x, y *complexRat) *complexRat {
z := newComplexRat()
denominator := new(big.Rat)
t := new(big.Rat)
t.Mul(y.r, y.r)
denominator.Mul(y.i, y.i)
denominator.Add(denominator, t)
if denominator.Cmp(zero) == 0 {
return newComplexRat()
}
ac := new(big.Rat)
bd := new(big.Rat)
ac.Mul(x.r, y.r)
bd.Mul(x.i, y.i)
bc := new(big.Rat)
ad := new(big.Rat)
bc.Mul(x.i, y.r)
ad.Mul(x.r, y.i)
z.r.Add(ac, bd)
z.r.Quo(z.r, denominator)
z.i.Add(bc, ad.Neg(ad))
z.i.Quo(z.i, denominator)
return z
}
示例4: binaryFloatOp
func binaryFloatOp(x *big.Rat, op token.Token, y *big.Rat) interface{} {
var z big.Rat
switch op {
case token.ADD:
return z.Add(x, y)
case token.SUB:
return z.Sub(x, y)
case token.MUL:
return z.Mul(x, y)
case token.QUO:
return z.Quo(x, y)
case token.EQL:
return x.Cmp(y) == 0
case token.NEQ:
return x.Cmp(y) != 0
case token.LSS:
return x.Cmp(y) < 0
case token.LEQ:
return x.Cmp(y) <= 0
case token.GTR:
return x.Cmp(y) > 0
case token.GEQ:
return x.Cmp(y) >= 0
}
panic("unreachable")
}
示例5: 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("")
}
}
}
}
示例6: renderRat
func renderRat(img *image.RGBA) {
var yminR, ymaxMinR, heightR big.Rat
yminR.SetInt64(ymin)
ymaxMinR.SetInt64(ymax - ymin)
heightR.SetInt64(height)
var xminR, xmaxMinR, widthR big.Rat
xminR.SetInt64(xmin)
xmaxMinR.SetInt64(xmax - xmin)
widthR.SetInt64(width)
var y, x big.Rat
for py := int64(0); py < height; py++ {
// y := float64(py)/height*(ymax-ymin) + ymin
y.SetInt64(py)
y.Quo(&y, &heightR)
y.Mul(&y, &ymaxMinR)
y.Add(&y, &yminR)
for px := int64(0); px < width; px++ {
// x := float64(px)/width*(xmax-xmin) + xmin
x.SetInt64(px)
x.Quo(&x, &widthR)
x.Mul(&x, &xmaxMinR)
x.Add(&x, &xminR)
c := mandelbrotRat(&x, &y)
if c == nil {
c = color.Black
}
img.Set(int(px), int(py), c)
}
}
}
示例7: cumulFlows
func cumulFlows(flows []*types.Flow) (bals []*types.Amount) {
x := new(big.Rat)
for _, f := range flows {
x = x.Add(x, (*big.Rat)(f.Price))
bals = append(bals, new(types.Amount).SetRat(x))
}
return
}
示例8: applyOp
func applyOp(val *big.Rat, op ast.OpClass, operand *big.Rat) {
switch op {
case ast.OpAdd:
val.Add(val, operand)
case ast.OpSubtract:
val.Sub(val, operand)
default:
panic(fmt.Sprintf("eval.applyOp: unkown operand: %d (%s)", op, op))
}
}
示例9: Round
func Round(r *big.Rat) *big.Rat {
d := new(big.Int).Set(r.Denom())
n := new(big.Int).Set(r.Num())
n.Mod(n, d)
if new(big.Int).Mul(n, big.NewInt(2)).Cmp(d) >= 0 {
r.Add(r, new(big.Rat).SetInt64(1))
}
r.Sub(r, new(big.Rat).SetFrac(n, d))
return r
}
示例10: runBilling
func runBilling(cmd *Command, rawArgs []string) error {
if billingHelp {
return cmd.PrintUsage()
}
if len(rawArgs) > 0 {
return cmd.PrintShortUsage()
}
// cli parsing
args := commands.PsArgs{
NoTrunc: billingNoTrunc,
}
ctx := cmd.GetContext(rawArgs)
logrus.Warn("")
logrus.Warn("Warning: 'scw _billing' is a work-in-progress price estimation tool")
logrus.Warn("For real usage, visit https://cloud.scaleway.com/#/billing")
logrus.Warn("")
// table
w := tabwriter.NewWriter(ctx.Stdout, 20, 1, 3, ' ', 0)
defer w.Flush()
fmt.Fprintf(w, "ID\tNAME\tSTARTED\tMONTH PRICE\n")
// servers
servers, err := cmd.API.GetServers(true, 0)
if err != nil {
return err
}
totalMonthPrice := new(big.Rat)
for _, server := range *servers {
if server.State != "running" {
continue
}
commercialType := strings.ToLower(server.CommercialType)
shortID := utils.TruncIf(server.Identifier, 8, !args.NoTrunc)
shortName := utils.TruncIf(utils.Wordify(server.Name), 25, !args.NoTrunc)
modificationTime, _ := time.Parse("2006-01-02T15:04:05.000000+00:00", server.ModificationDate)
modificationAgo := time.Now().UTC().Sub(modificationTime)
shortModificationDate := units.HumanDuration(modificationAgo)
usage := pricing.NewUsageByPath(fmt.Sprintf("/compute/%s/run", commercialType))
usage.SetStartEnd(modificationTime, time.Now().UTC())
totalMonthPrice = totalMonthPrice.Add(totalMonthPrice, usage.Total())
fmt.Fprintf(w, "server/%s/%s\t%s\t%s\t%s\n", commercialType, shortID, shortName, shortModificationDate, usage.TotalString())
}
fmt.Fprintf(w, "TOTAL\t\t\t%s\n", pricing.PriceString(totalMonthPrice, "EUR"))
return nil
}
示例11: Mul
func (z *BigComplex) Mul(x, y *BigComplex) *BigComplex {
re := new(big.Rat).Mul(&x.Re, &y.Re)
re.Sub(re, new(big.Rat).Mul(&x.Im, &y.Im))
im := new(big.Rat).Mul(&x.Re, &y.Im)
im.Add(im, new(big.Rat).Mul(&x.Im, &y.Re))
z.Re = *re
z.Im = *im
return z
}
示例12: Dot
func (v1 Vector) Dot(v2 Vector) *big.Rat {
if len(v1) != len(v2) {
log.Fatalf("Lengths differ: %d != %d", len(v1), len(v2))
}
d := new(big.Rat)
c := new(big.Rat)
for i, e := range v1 {
d.Add(d, c.Mul(e, v2[i]))
}
return d
}
示例13: Newt_Sqrt
func Newt_Sqrt(a *big.Rat, x *big.Rat) *big.Rat {
for i := 0; i < 12; i++ {
var quot1 = big.NewRat(1, 1)
quot1 = quot1.Quo(x, big.NewRat(2, 1))
var mul = big.NewRat(1, 1)
mul = mul.Mul(big.NewRat(2, 1), x)
var quot2 = big.NewRat(1, 1)
quot2 = quot2.Quo(a, mul)
x = x.Add(quot1, quot2)
}
return x
}
示例14: abs
func abs(x *complexRat) *big.Rat {
r := new(big.Rat)
i := new(big.Rat)
r.Set(x.r)
i.Set(x.i)
r.Mul(r, x.r) // r^2
i.Mul(i, x.i) // i^2
r.Add(r, i) // r^2 + i^2
return sqrtFloat(r) // sqrt(r^2 + i^2)
}
示例15: sexToDec
func sexToDec(deg, min, sec *big.Rat, dir string) *big.Rat {
// sexagesimal (base 60) to decimal
// https://imm.dtf.wa.gov.au/helpfiles/Latitude_Longitude_conversion_hlp.htm
deg.Add(deg, min.Quo(min, big.NewRat(60, 1)))
deg.Add(deg, sec.Quo(sec, big.NewRat(3600, 1)))
// N and E are the positive directions (like on an x,y axis)
if dir == "S" || dir == "W" {
deg.Neg(deg)
}
return deg
}