本文整理汇总了Golang中math/big.Rat.Mul方法的典型用法代码示例。如果您正苦于以下问题:Golang Rat.Mul方法的具体用法?Golang Rat.Mul怎么用?Golang Rat.Mul使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Rat
的用法示例。
在下文中一共展示了Rat.Mul方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: parseFilesize
// parseFilesize converts strings of form 10GB to a size in bytes. Fractional
// sizes are truncated at the byte size.
func parseFilesize(strSize string) (string, error) {
units := []struct {
suffix string
multiplier int64
}{
{"kb", 1e3},
{"mb", 1e6},
{"gb", 1e9},
{"tb", 1e12},
{"kib", 1 << 10},
{"mib", 1 << 20},
{"gib", 1 << 30},
{"tib", 1 << 40},
{"b", 1}, // must be after others else it'll match on them all
{"", 1}, // no suffix is still a valid suffix
}
strSize = strings.ToLower(strSize)
for _, unit := range units {
if strings.HasSuffix(strSize, unit.suffix) {
r, ok := new(big.Rat).SetString(strings.TrimSuffix(strSize, unit.suffix))
if !ok {
return "", errUnableToParseSize
}
r.Mul(r, new(big.Rat).SetInt(big.NewInt(unit.multiplier)))
if !r.IsInt() {
f, _ := r.Float64()
return fmt.Sprintf("%d", int64(f)), nil
}
return r.RatString(), nil
}
}
return "", errUnableToParseSize
}
示例2: 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)
}
}
}
示例3: 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")
}
示例4: hostconfigcmd
// hostconfigcmd is the handler for the command `siac host config [setting] [value]`.
// Modifies host settings.
func hostconfigcmd(param, value string) {
switch param {
case "price":
// convert price to hastings/byte/block
p, ok := new(big.Rat).SetString(value)
if !ok {
die("Could not parse price")
}
p.Mul(p, big.NewRat(1e24/1e9, 4320))
value = new(big.Int).Div(p.Num(), p.Denom()).String()
case "totalstorage":
// parse sizes of form 10GB, 10TB, 1TiB etc
var err error
value, err = parseSize(value)
if err != nil {
die("Could not parse totalstorage:", err)
}
case "minduration", "maxduration", "windowsize", "acceptingcontracts": // Other valid settings.
default:
// Reject invalid host config commands.
die("\"" + param + "\" is not a host setting")
}
err := post("/host", param+"="+value)
if err != nil {
die("Could not update host settings:", err)
}
fmt.Println("Host settings updated.")
}
示例5: coinUnits
// coinUnits converts a siacoin amount to base units.
func coinUnits(amount string) (string, error) {
units := []string{"pS", "nS", "uS", "mS", "SC", "KS", "MS", "GS", "TS"}
for i, unit := range units {
if strings.HasSuffix(amount, unit) {
// scan into big.Rat
r, ok := new(big.Rat).SetString(strings.TrimSuffix(amount, unit))
if !ok {
return "", errors.New("malformed amount")
}
// convert units
exp := 24 + 3*(int64(i)-4)
mag := new(big.Int).Exp(big.NewInt(10), big.NewInt(exp), nil)
r.Mul(r, new(big.Rat).SetInt(mag))
// r must be an integer at this point
if !r.IsInt() {
return "", errors.New("non-integer number of hastings")
}
return r.RatString(), nil
}
}
// check for hastings separately
if strings.HasSuffix(amount, "H") {
return strings.TrimSuffix(amount, "H"), nil
}
return "", errors.New("amount is missing units; run 'wallet --help' for a list of units")
}
示例6: hostconfigcmd
func hostconfigcmd(param, value string) {
// convert price to hastings/byte/block
if param == "price" {
p, ok := new(big.Rat).SetString(value)
if !ok {
fmt.Println("could not parse price")
return
}
p.Mul(p, big.NewRat(1e24/1e9, 4320))
value = new(big.Int).Div(p.Num(), p.Denom()).String()
}
// parse sizes of form 10GB, 10TB, 1TiB etc
if param == "totalstorage" {
var err error
value, err = parseSize(value)
if err != nil {
fmt.Println("could not parse " + param)
}
}
err := post("/host", param+"="+value)
if err != nil {
fmt.Println("Could not update host settings:", err)
return
}
fmt.Println("Host settings updated.")
}
示例7: Mul
func Mul(a, b string) string {
ra := new(big.Rat)
rb := new(big.Rat)
ra.SetString(a)
rb.SetString(b)
return ra.Mul(ra, rb).FloatString(4)
}
示例8: GasPrice
func GasPrice(bp, gl, ep *big.Int) *big.Int {
BP := new(big.Rat).SetInt(bp)
GL := new(big.Rat).SetInt(gl)
EP := new(big.Rat).SetInt(ep)
GP := new(big.Rat).Quo(BP, GL)
GP = GP.Quo(GP, EP)
return GP.Mul(GP, etherInWei).Num()
}
示例9: Mean
func (me *StatisticalAccumulator) Mean() *big.Rat {
mean := new(big.Rat)
mean.Inv(me.n)
mean.Mul(mean, me.sigmaXI)
return mean
}
示例10: tans
func tans(m []mTerm) *big.Rat {
if len(m) == 1 {
return tanEval(m[0].a, big.NewRat(m[0].n, m[0].d))
}
half := len(m) / 2
a := tans(m[:half])
b := tans(m[half:])
r := new(big.Rat)
return r.Quo(new(big.Rat).Add(a, b), r.Sub(one, r.Mul(a, b)))
}
示例11: 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
}
示例12: ParseCPUs
// ParseCPUs takes a string ratio and returns an integer value of nano cpus
func ParseCPUs(value string) (int64, error) {
cpu, ok := new(big.Rat).SetString(value)
if !ok {
return 0, fmt.Errorf("failed to parse %v as a rational number", value)
}
nano := cpu.Mul(cpu, big.NewRat(1e9, 1))
if !nano.IsInt() {
return 0, fmt.Errorf("value is too precise")
}
return nano.Num().Int64(), nil
}
示例13: ParseTimeDuration
// Returns the parsed duration in nanoseconds, support 'u', 's', 'm',
// 'h', 'd', 'W', 'M', and 'Y' suffixes.
func ParseTimeDuration(value string) (int64, error) {
var constant time.Duration
prefixSize := 1
switch value[len(value)-1] {
case 'u':
constant = time.Microsecond
case 's':
constant = time.Second
case 'm':
constant = time.Minute
case 'h':
constant = time.Hour
case 'd':
constant = 24 * time.Hour
case 'w', 'W':
constant = Week
case 'M':
constant = Month
case 'y', 'Y':
constant = Year
default:
prefixSize = 0
}
if value[len(value)-2:] == "ms" {
constant = time.Millisecond
prefixSize = 2
}
t := big.Rat{}
timeString := value
if prefixSize > 0 {
timeString = value[:len(value)-prefixSize]
}
_, err := fmt.Sscan(timeString, &t)
if err != nil {
return 0, err
}
if prefixSize > 0 {
c := big.Rat{}
c.SetFrac64(int64(constant), 1)
t.Mul(&t, &c)
}
if t.IsInt() {
return t.Num().Int64(), nil
}
f, _ := t.Float64()
return int64(f), nil
}
示例14: PutRat
func (me *StatisticalAccumulator) PutRat(x *big.Rat) {
me.n.Add(me.n, me.one)
me.sigmaXI.Add(me.sigmaXI, x)
xSquared := new(big.Rat)
xSquared.Mul(x, x)
me.sigmaXISquared.Add(me.sigmaXISquared, xSquared)
}
示例15: floatString
// floatString returns the string representation for a
// numeric value v in normalized floating-point format.
func floatString(v exact.Value) string {
if exact.Sign(v) == 0 {
return "0.0"
}
// x != 0
// convert |v| into a big.Rat x
x := new(big.Rat).SetFrac(absInt(exact.Num(v)), absInt(exact.Denom(v)))
// normalize x and determine exponent e
// (This is not very efficient, but also not speed-critical.)
var e int
for x.Cmp(ten) >= 0 {
x.Quo(x, ten)
e++
}
for x.Cmp(one) < 0 {
x.Mul(x, ten)
e--
}
// TODO(gri) Values such as 1/2 are easier to read in form 0.5
// rather than 5.0e-1. Similarly, 1.0e1 is easier to read as
// 10.0. Fine-tune best exponent range for readability.
s := x.FloatString(100) // good-enough precision
// trim trailing 0's
i := len(s)
for i > 0 && s[i-1] == '0' {
i--
}
s = s[:i]
// add a 0 if the number ends in decimal point
if len(s) > 0 && s[len(s)-1] == '.' {
s += "0"
}
// add exponent and sign
if e != 0 {
s += fmt.Sprintf("e%+d", e)
}
if exact.Sign(v) < 0 {
s = "-" + s
}
// TODO(gri) If v is a "small" fraction (i.e., numerator and denominator
// are just a small number of decimal digits), add the exact fraction as
// a comment. For instance: 3.3333...e-1 /* = 1/3 */
return s
}