本文整理汇总了Golang中math/big.Int.Div方法的典型用法代码示例。如果您正苦于以下问题:Golang Int.Div方法的具体用法?Golang Int.Div怎么用?Golang Int.Div使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Int
的用法示例。
在下文中一共展示了Int.Div方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: Encode
// Encode encodes src into EncodedMaxLen(len(src))
// or fewer bytes of dst. It returns the number of bytes written to dst.
func Encode(dst, src []byte) int {
zeros := 0
for _, b := range src {
if int(b) == 0 {
zeros++
} else {
break
}
}
i := new(big.Int).SetBytes(src)
big58 := big.NewInt(58)
big0 := big.NewInt(0)
var index int
for i.Cmp(big0) > 0 {
tmp := new(big.Int).Mod(i, big58)
i.Div(i, big58)
dst[index] = base58alphabet[tmp.Int64()]
index++
}
for ; zeros > 0; zeros-- {
dst[index] = base58alphabet[0]
index++
}
reverseInplace(dst[0:index])
return index
}
示例3: SuggestPrice
func (self *GasPriceOracle) SuggestPrice() *big.Int {
self.lastBaseMutex.Lock()
base := self.lastBase
self.lastBaseMutex.Unlock()
if base == nil {
base = self.eth.GpoMinGasPrice
}
if base == nil {
return big.NewInt(10000000000000) // apparently MinGasPrice is not initialized during some tests
}
baseCorr := new(big.Int).Mul(base, big.NewInt(int64(self.eth.GpobaseCorrectionFactor)))
baseCorr.Div(baseCorr, big.NewInt(100))
if baseCorr.Cmp(self.eth.GpoMinGasPrice) < 0 {
return self.eth.GpoMinGasPrice
}
if baseCorr.Cmp(self.eth.GpoMaxGasPrice) > 0 {
return self.eth.GpoMaxGasPrice
}
return baseCorr
}
示例4: calcDifficultyFrontier
func calcDifficultyFrontier(time, parentTime uint64, parentNumber, parentDiff *big.Int) *big.Int {
diff := new(big.Int)
adjust := new(big.Int).Div(parentDiff, params.DifficultyBoundDivisor)
bigTime := new(big.Int)
bigParentTime := new(big.Int)
bigTime.SetUint64(time)
bigParentTime.SetUint64(parentTime)
if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 {
diff.Add(parentDiff, adjust)
} else {
diff.Sub(parentDiff, adjust)
}
if diff.Cmp(params.MinimumDifficulty) < 0 {
diff.Set(params.MinimumDifficulty)
}
periodCount := new(big.Int).Add(parentNumber, common.Big1)
periodCount.Div(periodCount, ExpDiffPeriod)
if periodCount.Cmp(common.Big1) > 0 {
// diff = diff + 2^(periodCount - 2)
expDiff := periodCount.Sub(periodCount, common.Big2)
expDiff.Exp(common.Big2, expDiff, nil)
diff.Add(diff, expDiff)
diff = common.BigMax(diff, params.MinimumDifficulty)
}
return diff
}
示例5: ISqrt
// ISqrt returns the greatest number x such that x^2 <= n. n must be
// non-negative.
//
// See https://www.akalin.com/computing-isqrt for an analysis.
func ISqrt(n *big.Int) *big.Int {
s := n.Sign()
if s < 0 {
panic("negative radicand")
}
if s == 0 {
return &big.Int{}
}
// x = 2^ceil(Bits(n)/2)
var x big.Int
x.Lsh(big.NewInt(1), (uint(n.BitLen())+1)/2)
for {
// y = floor((x + floor(n/x))/2)
var y big.Int
y.Div(n, &x)
y.Add(&y, &x)
y.Rsh(&y, 1)
if y.Cmp(&x) >= 0 {
return &x
}
x = y
}
}
示例6: calcDiffAdjust
/* calcDiff returns a bool given two block headers. This bool is
true if the correct dificulty adjustment is seen in the "next" header.
Only feed it headers n-2016 and n-1, otherwise it will calculate a difficulty
when no adjustment should take place, and return false.
Note that the epoch is actually 2015 blocks long, which is confusing. */
func calcDiffAdjust(start, end wire.BlockHeader, p *chaincfg.Params) uint32 {
duration := end.Timestamp.UnixNano() - start.Timestamp.UnixNano()
if duration < minRetargetTimespan {
log.Printf("whoa there, block %s off-scale high 4X diff adjustment!",
end.BlockSha().String())
duration = minRetargetTimespan
} else if duration > maxRetargetTimespan {
log.Printf("Uh-oh! block %s off-scale low 0.25X diff adjustment!\n",
end.BlockSha().String())
duration = maxRetargetTimespan
}
// calculation of new 32-byte difficulty target
// first turn the previous target into a big int
prevTarget := blockchain.CompactToBig(start.Bits)
// new target is old * duration...
newTarget := new(big.Int).Mul(prevTarget, big.NewInt(duration))
// divided by 2 weeks
newTarget.Div(newTarget, big.NewInt(int64(targetTimespan)))
// clip again if above minimum target (too easy)
if newTarget.Cmp(p.PowLimit) > 0 {
newTarget.Set(p.PowLimit)
}
// calculate and return 4-byte 'bits' difficulty from 32-byte target
return blockchain.BigToCompact(newTarget)
}
示例7: Sign
func (d *biasedEcdsa) Sign(m []byte) (*big.Int, *big.Int) {
h := sha1.New()
if n, err := h.Write(m); n != len(m) || err != nil {
log.Fatal("Error calculating hash")
}
e := h.Sum(nil)
r, s := new(big.Int), new(big.Int)
n := d.g.Size()
z := new(big.Int).SetBytes(e)
z.Mod(z, n)
for r.Cmp(new(big.Int)) == 0 || s.Cmp(new(big.Int)) == 0 {
k := new(big.Int).Rand(rnd, n)
if k.Cmp(new(big.Int)) == 0 {
continue
}
log.Printf("Original k: %x", k)
k.Div(k, big.NewInt(1<<d.bias)).Mul(k, big.NewInt(1<<d.bias))
log.Printf("Biased k: %x", k)
p := d.g.Pow(d.g.Generator(), k)
r.Mod(p.(*ellipticCurveElement).x, n)
k.ModInverse(k, n)
s.Mul(r, d.key)
s.Add(s, z)
s.Mul(s, k)
s.Mod(s, n)
}
return r, s
}
示例8: CalcGasLimit
// CalcGasLimit computes the gas limit of the next block after parent.
// The result may be modified by the caller.
// This is miner strategy, not consensus protocol.
func CalcGasLimit(parent *types.Block) *big.Int {
// contrib = (parentGasUsed * 3 / 2) / 1024
contrib := new(big.Int).Mul(parent.GasUsed(), big.NewInt(3))
contrib = contrib.Div(contrib, big.NewInt(2))
contrib = contrib.Div(contrib, params.GasLimitBoundDivisor)
// decay = parentGasLimit / 1024 -1
decay := new(big.Int).Div(parent.GasLimit(), params.GasLimitBoundDivisor)
decay.Sub(decay, big.NewInt(1))
/*
strategy: gasLimit of block-to-mine is set based on parent's
gasUsed value. if parentGasUsed > parentGasLimit * (2/3) then we
increase it, otherwise lower it (or leave it unchanged if it's right
at that usage) the amount increased/decreased depends on how far away
from parentGasLimit * (2/3) parentGasUsed is.
*/
gl := new(big.Int).Sub(parent.GasLimit(), decay)
gl = gl.Add(gl, contrib)
gl.Set(common.BigMax(gl, params.MinGasLimit))
// however, if we're now below the target (TargetGasLimit) we increase the
// limit as much as we can (parentGasLimit / 1024 -1)
if gl.Cmp(params.TargetGasLimit) < 0 {
gl.Add(parent.GasLimit(), decay)
gl.Set(common.BigMin(gl, params.TargetGasLimit))
}
return gl
}
示例9: Think
func (o *CombatObject) Think() {
o.LivingObject.Think()
max := o.Outer().(Combat).MaxHealth()
o.mtx.Lock()
if o.combatTicks > 0 {
o.combatTicks--
}
if o.damaged.Sign() > 0 && o.damaged.Cmp(max) < 0 {
var regen big.Int
if o.combatTicks > 0 {
regen.Div(o.Outer().(StatLike).Stat(StatHealthRegen), TuningHealthRegenDivisorCombat)
} else {
regen.Div(o.Outer().(StatLike).Stat(StatHealthRegen), TuningHealthRegenDivisorNonCombat)
}
o.damaged.Sub(&o.damaged, ®en)
if o.damaged.Sign() < 0 {
o.damaged.SetUint64(0)
}
if pos := o.Position(); pos != nil {
o.mtx.Unlock()
pos.Zone().Update(pos, o.Outer())
return
}
}
if o.damaged.Cmp(max) >= 0 {
o.mtx.Unlock()
o.Outer().(Combat).Die()
return
}
o.mtx.Unlock()
}
示例10: normalizeBigInt
// normalizeBigInt divides off all trailing zeros from the provided big.Int.
// It will only modify the provided big.Int if copyOnWrite is not set, and
// it will use the formatted representation of the big.Int if it is provided.
func normalizeBigInt(bi *big.Int, copyOnWrite bool, formatted, tmp []byte) *big.Int {
tens := 0
if formatted != nil {
tens = trailingZerosFromBytes(formatted)
} else {
tens = trailingZeros(bi, tmp)
}
if tens > 0 {
// If the decimal's big.Int hasn't been copied already, copy
// it now because we will be modifying it.
from := bi
if copyOnWrite {
bi = new(big.Int)
}
var div *big.Int
switch tens {
case 1:
div = bigInt10
case 2:
div = bigInt100
case 3:
div = bigInt1000
default:
div = big.NewInt(10)
pow := big.NewInt(int64(tens))
div.Exp(div, pow, nil)
}
bi.Div(from, div)
}
return bi
}
示例11: String
// String returns a float string representation of a Decimal
func (d Decimal) String() string {
// Retrieve a copy of the Decimal's internal big.Rat denominator
denom := new(big.Int)
denom.Set(d.rational.Denom())
// Discover the precision of the denominator and use it to fix
// the precision of the string conversion
var precision = 0
one := big.NewInt(1)
ten := big.NewInt(10)
for denom.Cmp(one) > 0 {
denom = denom.Div(denom, ten)
precision++
}
if !d.finite {
if d.rational.Sign() == 1 {
return "Infinity"
} else if d.rational.Sign() == -1 {
return "-Infinity"
} else {
return "NaN"
}
}
return d.rational.FloatString(precision)
}
示例12: Comma
// Ported to math/big.Int from github.com/dustin/go-humanize
func Comma(v *big.Int) string {
{
var copy big.Int
copy.Set(v)
v = ©
}
sign := ""
if v.Sign() < 0 {
sign = "-"
v.Abs(v)
}
tmp := &big.Int{}
herman := big.NewInt(999)
thousand := big.NewInt(1000)
var parts []string
for v.Cmp(herman) > 0 {
part := tmp.Mod(v, thousand).String()
switch len(part) {
case 2:
part = "0" + part
case 1:
part = "00" + part
}
v.Div(v, thousand)
parts = append(parts, part)
}
parts = append(parts, v.String())
for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 {
parts[i], parts[j] = parts[j], parts[i]
}
return sign + strings.Join(parts, ",")
}
示例13: NewPaillierPrivateKey
// NewPaillierPrivateKey generates a new Paillier private key (key pair).
//
// The key used in the Paillier crypto system consists of four integer
// values. The public key has two parameters; the private key has three
// parameters (one parameter is shared between the keys). As in RSA it
// starts with two random primes 'p' and 'q'; the public key parameter
// are computed as:
//
// n := p * q
// g := random number from interval [0,n^2[
//
// The private key parameters are computed as:
//
// n := p * q
// l := lcm (p-1,q-1)
// u := (((g^l mod n^2)-1)/n) ^-1 mod n
//
// N.B. The division by n is integer based and rounds toward zero!
func NewPaillierPrivateKey(bits int) (key *PaillierPrivateKey, err error) {
// generate primes 'p' and 'q' and their factor 'n'
// repeat until the requested factor bitsize is reached
var p, q, n *big.Int
for {
bitsP := (bits - 5) / 2
bitsQ := bits - bitsP
p, err = rand.Prime(rand.Reader, bitsP)
if err != nil {
return nil, err
}
q, err = rand.Prime(rand.Reader, bitsQ)
if err != nil {
return nil, err
}
n = new(big.Int).Mul(p, q)
if n.BitLen() == bits {
break
}
}
// initialize variables
one := big.NewInt(1)
n2 := new(big.Int).Mul(n, n)
// compute public key parameter 'g' (generator)
g, err := rand.Int(rand.Reader, n2)
if err != nil {
return nil, err
}
// compute private key parameters
p1 := new(big.Int).Sub(p, one)
q1 := new(big.Int).Sub(q, one)
l := new(big.Int).Mul(q1, p1)
l.Div(l, new(big.Int).GCD(nil, nil, p1, q1))
a := new(big.Int).Exp(g, l, n2)
a.Sub(a, one)
a.Div(a, n)
u := new(big.Int).ModInverse(a, n)
// return key pair
pubkey := &PaillierPublicKey{
N: n,
G: g,
}
prvkey := &PaillierPrivateKey{
PaillierPublicKey: pubkey,
L: l,
U: u,
P: p,
Q: q,
}
return prvkey, nil
}
示例14: c
func c(n int64, k int64) *big.Int {
var result *big.Int
var denominator big.Int
result = big.NewInt(0)
denominator.Mul(fact(k), fact(n-k))
result.Div(fact(n), &denominator)
return result
}
示例15: safe
func (t *lft) safe(n *big.Int) bool {
r := t.extr(four)
var f big.Int
if n.Cmp(f.Div(r.Num(), r.Denom())) == 0 {
return true
}
return false
}