本文整理汇总了Golang中math/big.Rat.Num方法的典型用法代码示例。如果您正苦于以下问题:Golang Rat.Num方法的具体用法?Golang Rat.Num怎么用?Golang Rat.Num使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Rat
的用法示例。
在下文中一共展示了Rat.Num方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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.")
}
示例2: ratmod
func ratmod(x, y *big.Rat) *big.Rat {
if x.IsInt() && y.IsInt() {
return new(big.Rat).SetInt(new(big.Int).Mod(x.Num(), y.Num()))
}
panic("operation not permitted")
}
示例3: floor
func floor(n *big.Rat) *big.Rat {
f := &big.Rat{}
z := new(big.Int)
z.Div(n.Num(), n.Denom())
f.SetInt(z)
return f
}
示例4: 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.")
}
示例5: ratCeil
// Returns a new big.Int set to the ceiling of x.
func ratCeil(x *big.Rat) *big.Int {
z := new(big.Int)
m := new(big.Int)
z.DivMod(x.Num(), x.Denom(), m)
if m.Cmp(intZero) == 1 {
z.Add(z, intOne)
}
return z
}
示例6: MulRat
// MulRat returns a new Currency value c = x * y, where y is a big.Rat.
func (x Currency) MulRat(y *big.Rat) (c Currency) {
if y.Sign() < 0 {
build.Critical(ErrNegativeCurrency)
} else {
c.i.Mul(&x.i, y.Num())
c.i.Div(&c.i, y.Denom())
}
return
}
示例7: 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
}
示例8: CalcGasLimit
func CalcGasLimit(parent *types.Block) *big.Int {
// ((1024-1) * parent.gasLimit + (gasUsed * 6 / 5)) / 1024
previous := new(big.Int).Mul(big.NewInt(1024-1), parent.GasLimit())
current := new(big.Rat).Mul(new(big.Rat).SetInt(parent.GasUsed()), big.NewRat(6, 5))
curInt := new(big.Int).Div(current.Num(), current.Denom())
result := new(big.Int).Add(previous, curInt)
result.Div(result, big.NewInt(1024))
return common.BigMax(params.GenesisGasLimit, result)
}
示例9: MulRat
// MulRat returns a new Currency value c = x * y, where y is a big.Rat.
func (x Currency) MulRat(y *big.Rat) (c Currency) {
if y.Sign() < 0 {
if build.DEBUG {
panic(ErrNegativeCurrency)
}
} else {
c.i.Mul(&x.i, y.Num())
c.i.Div(&c.i, y.Denom())
}
return
}
示例10: 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
}
示例11: RatToTarget
// RatToTarget converts a big.Rat to a Target.
func RatToTarget(r *big.Rat) (t Target) {
if r.Num().Sign() < 0 {
if build.DEBUG {
panic(ErrNegativeTarget)
}
} else {
i := new(big.Int).Div(r.Num(), r.Denom())
t = IntToTarget(i)
}
return
}
示例12: isSolution
// Do the numerator and denominator of r provide a solution to the equation
// x**2 - D*(y**2) = 1?
func isSolution(D int, r *big.Rat) bool {
S := big.NewInt(int64(D))
x := r.Num()
y := r.Denom()
one := big.NewInt(1)
two := big.NewInt(2)
a := new(big.Int).Exp(x, two, nil)
b := new(big.Int).Exp(y, two, nil)
return new(big.Int).Sub(a, new(big.Int).Mul(S, b)).Cmp(one) == 0
}
示例13: MulFloat
// COMPATv0.4.0 - until the first 10e3 blocks have been archived, MulFloat is
// needed while verifying the first set of blocks.
//
// MulFloat returns a new Currency value y = c * x, where x is a float64.
// Behavior is undefined when x is negative.
func (x Currency) MulFloat(y float64) (c Currency) {
if y < 0 {
build.Critical(ErrNegativeCurrency)
} else {
cRat := new(big.Rat).Mul(
new(big.Rat).SetInt(&x.i),
new(big.Rat).SetFloat64(y),
)
c.i.Div(cRat.Num(), cRat.Denom())
}
return
}
示例14: makeRat
func makeRat(x *big.Rat) Value {
a := x.Num()
b := x.Denom()
if a.BitLen() < maxExp && b.BitLen() < maxExp {
// ok to remain fraction
return ratVal{x}
}
// components too large => switch to float
fa := newFloat().SetInt(a)
fb := newFloat().SetInt(b)
return floatVal{fa.Quo(fa, fb)}
}
示例15: NewRational
func NewRational(r *big.Rat) Rational {
if !r.IsInt() || r.Cmp(min) < 0 || r.Cmp(max) > 0 {
return Rational{r}
}
n := r.Num().Int64()
i := n + 256
p := rat[i]
if p.v == nil {
p = Rational{r}
rat[i] = p
}
return p
}