当前位置: 首页>>代码示例>>Golang>>正文


Golang Int.Neg方法代码示例

本文整理汇总了Golang中math/big.Int.Neg方法的典型用法代码示例。如果您正苦于以下问题:Golang Int.Neg方法的具体用法?Golang Int.Neg怎么用?Golang Int.Neg使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在math/big.Int的用法示例。


在下文中一共展示了Int.Neg方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: readBigInt

func readBigInt(r io.Reader, b []byte, sign byte) (interface{}, error) {
	if _, err := io.ReadFull(r, b); err != nil {
		return nil, err
	}

	size := len(b)
	hsize := size >> 1
	for i := 0; i < hsize; i++ {
		b[i], b[size-i-1] = b[size-i-1], b[i]
	}

	v := new(big.Int).SetBytes(b)
	if sign != 0 {
		v = v.Neg(v)
	}

	// try int and int64
	v64 := v.Int64()
	if x := int(v64); v.Cmp(big.NewInt(int64(x))) == 0 {
		return x, nil
	} else if v.Cmp(big.NewInt(v64)) == 0 {
		return v64, nil
	}

	return v, nil
}
开发者ID:agelin,项目名称:etf,代码行数:26,代码来源:read.go

示例2: roundHalf

func roundHalf(f func(c int, odd uint) (roundUp bool)) func(z, q *Dec, rA, rB *big.Int) *Dec {
	return func(z, q *Dec, rA, rB *big.Int) *Dec {
		z.Set(q)
		brA, brB := rA.BitLen(), rB.BitLen()
		if brA < brB-1 {
			// brA < brB-1 => |rA| < |rB/2|
			return z
		}
		roundUp := false
		srA, srB := rA.Sign(), rB.Sign()
		s := srA * srB
		if brA == brB-1 {
			rA2 := new(big.Int).Lsh(rA, 1)
			if s < 0 {
				rA2.Neg(rA2)
			}
			roundUp = f(rA2.Cmp(rB)*srB, z.UnscaledBig().Bit(0))
		} else {
			// brA > brB-1 => |rA| > |rB/2|
			roundUp = true
		}
		if roundUp {
			z.UnscaledBig().Add(z.UnscaledBig(), intSign[s+1])
		}
		return z
	}
}
开发者ID:40a,项目名称:bootkube,代码行数:27,代码来源:rounder.go

示例3: ToBase

// ToBase produces n in base b. For example
//
// 	ToBase(2047, 22) -> [1, 5, 4]
//
//	1 * 22^0           1
//	5 * 22^1         110
//	4 * 22^2        1936
//	                ----
//	                2047
//
// ToBase panics for bases < 2.
func ToBase(n *big.Int, b int) []int {
	var nn big.Int
	nn.Set(n)
	if b < 2 {
		panic("invalid base")
	}

	k := 1
	switch nn.Sign() {
	case -1:
		nn.Neg(&nn)
		k = -1
	case 0:
		return []int{0}
	}

	bb := big.NewInt(int64(b))
	var r []int
	rem := big.NewInt(0)
	for nn.Sign() != 0 {
		nn.QuoRem(&nn, bb, rem)
		r = append(r, k*int(rem.Int64()))
	}
	return r
}
开发者ID:cznic,项目名称:mathutil,代码行数:36,代码来源:mathutil.go

示例4: String

func (v *Value) String() string {
	if v.IsZero() {
		return "0"
	}
	if v.Native && v.IsScientific() {
		value := strconv.FormatUint(v.Num, 10)
		offset := strconv.FormatInt(v.Offset, 10)
		if v.Negative {
			return "-" + value + "e" + offset
		}
		return value + "e" + offset
	}
	value := big.NewInt(int64(v.Num))
	if v.Negative {
		value.Neg(value)
	}
	var offset *big.Int
	if v.Native {
		offset = big.NewInt(-6)
	} else {
		offset = big.NewInt(v.Offset)
	}
	exp := offset.Exp(bigTen, offset.Neg(offset), nil)
	rat := big.NewRat(0, 1).SetFrac(value, exp)
	left := rat.FloatString(0)
	if rat.IsInt() {
		return left
	}
	length := len(left)
	if v.Negative {
		length -= 1
	}
	return strings.TrimRight(rat.FloatString(32-length), "0")
}
开发者ID:robcat,项目名称:ripple,代码行数:34,代码来源:amount.go

示例5: TestPoint

// TestPoint verifies that a point (x1,y2) does not equal another point (x1,y2) or it's reflection on 0
func (curve Curve) TestPoint(x1, y1, x2, y2 *big.Int) (bool, error) {
	_, err := curve.TestCoordinate(x1)
	if err != nil {
		return false, err
	}
	_, err = curve.TestCoordinate(x2)
	if err != nil {
		return false, err
	}
	if x1.Cmp(x1) == 0 && y1.Cmp(y2) == 0 { // Same
		return false, ErrCoordinateBase
	}
	if x1.Cmp(y1) == 0 && y1.Cmp(x2) == 0 { // Reflect
		return false, ErrCoordinateBase
	}
	x2neg := new(big.Int)
	x2neg = x2neg.Neg(x2)
	y2neg := new(big.Int)
	y2neg = y2neg.Neg(y2)
	if x1.Cmp(x2neg) == 0 {
		return false, ErrCoordinateBase
	}
	if x1.Cmp(y2neg) == 0 && y1.Cmp(x2neg) == 0 {
		return false, ErrCoordinateBase
	}
	return true, nil
}
开发者ID:JonathanLogan,项目名称:mute,代码行数:28,代码来源:eccutil.go

示例6: CompactToBig

// CompactToBig converts a compact representation of a whole number N to an
// unsigned 32-bit number.  The representation is similar to IEEE754 floating
// point numbers.
//
// Like IEEE754 floating point, there are three basic components: the sign,
// the exponent, and the mantissa.  They are broken out as follows:
//
//	* the most significant 8 bits represent the unsigned base 256 exponent
// 	* bit 23 (the 24th bit) represents the sign bit
//	* the least significant 23 bits represent the mantissa
//
//	-------------------------------------------------
//	|   Exponent     |    Sign    |    Mantissa     |
//	-------------------------------------------------
//	| 8 bits [31-24] | 1 bit [23] | 23 bits [22-00] |
//	-------------------------------------------------
//
// The formula to calculate N is:
// 	N = (-1^sign) * mantissa * 256^(exponent-3)
//
// This compact form is only used in bitcoin to encode unsigned 256-bit numbers
// which represent difficulty targets, thus there really is not a need for a
// sign bit, but it is implemented here to stay consistent with bitcoind.
func CompactToBig(compact uint32) *big.Int {
	// Extract the mantissa, sign bit, and exponent.
	mantissa := compact & 0x007fffff
	isNegative := compact&0x00800000 != 0
	exponent := uint(compact >> 24)

	// Since the base for the exponent is 256, the exponent can be treated
	// as the number of bytes to represent the full 256-bit number.  So,
	// treat the exponent as the number of bytes and shift the mantissa
	// right or left accordingly.  This is equivalent to:
	// N = mantissa * 256^(exponent-3)
	var bn *big.Int
	if exponent <= 3 {
		mantissa >>= 8 * (3 - exponent)
		bn = big.NewInt(int64(mantissa))
	} else {
		bn = big.NewInt(int64(mantissa))
		bn.Lsh(bn, 8*(exponent-3))
	}

	// Make it negative if the sign bit is set.
	if isNegative {
		bn = bn.Neg(bn)
	}

	return bn
}
开发者ID:rahulxkrishna,项目名称:btcd,代码行数:50,代码来源:difficulty.go

示例7: floatString

func (i BigInt) floatString(verb byte, prec int) string {
	switch verb {
	case 'f', 'F':
		str := fmt.Sprintf("%d", i.Int)
		if prec > 0 {
			str += "." + zeros(prec)
		}
		return str
	case 'e', 'E':
		// The exponent will alway be >= 0.
		sign := ""
		var x big.Int
		x.Set(i.Int)
		if x.Sign() < 0 {
			sign = "-"
			x.Neg(&x)
		}
		return eFormat(verb, prec, sign, x.String(), eExponent(&x))
	case 'g', 'G':
		// Exponent is always positive so it's easy.
		var x big.Int
		x.Set(i.Int)
		if eExponent(&x) >= prec {
			// Use e format.
			verb -= 2 // g becomes e.
			return trimEZeros(verb, i.floatString(verb, prec-1))
		}
		// Use f format, but this is just an integer.
		return fmt.Sprintf("%d", i.Int)
	default:
		Errorf("can't handle verb %c for big int", verb)
	}
	return ""
}
开发者ID:jmptrader,项目名称:ivy,代码行数:34,代码来源:bigint.go

示例8: Neg

// Neg() returns a polynomial Q = -P
func (p *Poly) Neg() Poly {
	var q Poly = make([]*big.Int, len(*p))
	for i := 0; i < len(*p); i++ {
		b := new(big.Int)
		b.Neg((*p)[i])
		q[i] = b
	}
	return q
}
开发者ID:obinnus,项目名称:polynomial,代码行数:10,代码来源:polynomial.go

示例9: unaryIntOp

func unaryIntOp(x *big.Int, op token.Token) interface{} {
	var z big.Int
	switch op {
	case token.ADD:
		return z.Set(x)
	case token.SUB:
		return z.Neg(x)
	case token.XOR:
		return z.Not(x)
	}
	panic("unreachable")
}
开发者ID:spate,项目名称:llgo,代码行数:12,代码来源:const.go

示例10: Bytes

func (d Decimal) Bytes() []byte {
	bytes := make([]byte, 16)
	binary.BigEndian.PutUint32(bytes[0:4], d.integer[3])
	binary.BigEndian.PutUint32(bytes[4:8], d.integer[2])
	binary.BigEndian.PutUint32(bytes[8:12], d.integer[1])
	binary.BigEndian.PutUint32(bytes[12:16], d.integer[0])
	var x big.Int
	x.SetBytes(bytes)
	if !d.positive {
		x.Neg(&x)
	}
	return scaleBytes(x.String(), d.scale)
}
开发者ID:citywander,项目名称:vault,代码行数:13,代码来源:decimal.go

示例11: trunc

// trunc truncates a value to the range of the given type.
func (t *_type) trunc(x *big.Int) *big.Int {
	r := new(big.Int)
	m := new(big.Int)
	m.Lsh(one, t.bits)
	m.Sub(m, one)
	r.And(x, m)
	if t.signed && r.Bit(int(t.bits)-1) == 1 {
		m.Neg(one)
		m.Lsh(m, t.bits)
		r.Or(r, m)
	}
	return r
}
开发者ID:Xiahl1990,项目名称:go,代码行数:14,代码来源:issue9604b.go

示例12: eExponent

// eExponent returns the exponent to use to display i in 1.23e+04 format.
func eExponent(x *big.Int) int {
	if x.Sign() < 0 {
		x.Neg(x)
	}
	e := 0
	for x.Cmp(bigIntBillion) >= 0 {
		e += 9
		x.Quo(x, bigIntBillion)
	}
	for x.Cmp(bigIntTen) >= 0 {
		e++
		x.Quo(x, bigIntTen)
	}
	return e
}
开发者ID:jmptrader,项目名称:ivy,代码行数:16,代码来源:bigint.go

示例13: testNegativeInputs

func testNegativeInputs(t *testing.T, curve elliptic.Curve, tag string) {
	key, err := GenerateKey(curve, rand.Reader)
	if err != nil {
		t.Errorf("failed to generate key for %q", tag)
	}

	var hash [32]byte
	r := new(big.Int).SetInt64(1)
	r.Lsh(r, 550 /* larger than any supported curve */)
	r.Neg(r)

	if Verify(&key.PublicKey, hash[:], r, r) {
		t.Errorf("bogus signature accepted for %q", tag)
	}
}
开发者ID:2thetop,项目名称:go,代码行数:15,代码来源:ecdsa_test.go

示例14: instantiateINTEGER

func instantiateINTEGER(inst *Instance, data interface{}, p *pathNode) (*Instance, error) {
	switch data := data.(type) {
	case *UnmarshalledPrimitive:
		if len(data.Data) == 0 {
			return nil, fmt.Errorf("%vAttempt to instantiate INTEGER from empty DER data", p)
		}
		var b big.Int
		b.SetBytes(data.Data)
		if data.Data[0]&128 != 0 { // negative number
			var x big.Int
			x.SetBit(&x, len(data.Data)*8, 1)
			x.Sub(&x, &b)
			b.Neg(&x)
		}
		return instantiateINTEGER(inst, &b, p)
	case *Instance:
		inst.value = data.value
	case *big.Int:
		i := int(data.Int64())
		if big.NewInt(int64(i)).Cmp(data) == 0 {
			inst.value = i // store as int if possible
		} else {
			inst.value = data // use *big.Int if necessary
		}
	case int:
		inst.value = data
	case float64:
		if math.Floor(data) != data {
			return nil, fmt.Errorf("%vAttempt to instantiate INTEGER with non-integral float64: %v", p, data)
		}
		inst.value = int(data)
	case string:
		if i, found := inst.namedints[data]; found {
			inst.value = i
		} else {
			bi := new(big.Int)
			_, ok := bi.SetString(data, 10)
			if ok {
				return instantiateINTEGER(inst, bi, p)
			} else {
				return nil, fmt.Errorf("%vAttempt to instantiate INTEGER/ENUMERATED from illegal string: %v", p, data)
			}
		}
	default:
		return nil, instantiateTypeError(p, "INTEGER", data)
	}
	return inst, nil
}
开发者ID:tomzhang,项目名称:certifidog,代码行数:48,代码来源:instantiate.go

示例15: parseBigInt

// parseBigInt treats the given bytes as a big-endian, signed integer and returns
// the result.
func parseBigInt(bytes []byte) *big.Int {
	ret := new(big.Int)
	if len(bytes) > 0 && bytes[0]&0x80 == 0x80 {
		// This is a negative number.
		notBytes := make([]byte, len(bytes))
		for i := range notBytes {
			notBytes[i] = ^bytes[i]
		}
		ret.SetBytes(notBytes)
		ret.Add(ret, bigOne)
		ret.Neg(ret)
		return ret
	}
	ret.SetBytes(bytes)
	return ret
}
开发者ID:h8liu,项目名称:golang,代码行数:18,代码来源:asn1.go


注:本文中的math/big.Int.Neg方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。