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


Golang inf.Scale函数代码示例

本文整理汇总了Golang中speter/net/go/exp/math/dec/inf.Scale函数的典型用法代码示例。如果您正苦于以下问题:Golang Scale函数的具体用法?Golang Scale怎么用?Golang Scale使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: TestDecGobEncoding

func TestDecGobEncoding(t *testing.T) {
	var medium bytes.Buffer
	enc := gob.NewEncoder(&medium)
	dec := gob.NewDecoder(&medium)
	for i, test := range decGobEncodingTests {
		for j := 0; j < 2; j++ {
			for k := inf.Scale(-5); k <= 5; k++ {
				medium.Reset() // empty buffer for each test case (in case of failures)
				stest := test
				if j != 0 {
					// negative numbers
					stest = "-" + test
				}
				var tx inf.Dec
				tx.SetString(stest)
				tx.SetScale(k) // test with positive, negative, and zero scale
				if err := enc.Encode(&tx); err != nil {
					t.Errorf("#%d%c: encoding failed: %s", i, 'a'+j, err)
				}
				var rx inf.Dec
				if err := dec.Decode(&rx); err != nil {
					t.Errorf("#%d%c: decoding failed: %s", i, 'a'+j, err)
				}
				if rx.Cmp(&tx) != 0 {
					t.Errorf("#%d%c: transmission failed: got %s want %s", i, 'a'+j, &rx, &tx)
				}
			}
		}
	}
}
开发者ID:RomainVabre,项目名称:origin,代码行数:30,代码来源:dec_test.go

示例2: ParseQuantity

// ParseQuantity turns str into a Quantity, or returns an error.
func ParseQuantity(str string) (*Quantity, error) {
	parts := splitRE.FindStringSubmatch(strings.TrimSpace(str))
	// regexp returns are entire match, followed by an entry for each () section.
	if len(parts) != 3 {
		return nil, ErrFormatWrong
	}

	amount := new(inf.Dec)
	if _, ok := amount.SetString(parts[1]); !ok {
		return nil, ErrNumeric
	}

	base, exponent, format, ok := quantitySuffixer.interpret(suffix(parts[2]))
	if !ok {
		return nil, ErrSuffix
	}

	// So that no one but us has to think about suffixes, remove it.
	if base == 10 {
		amount.SetScale(amount.Scale() + inf.Scale(-exponent))
	} else if base == 2 {
		// numericSuffix = 2 ** exponent
		numericSuffix := big.NewInt(1).Lsh(bigOne, uint(exponent))
		ub := amount.UnscaledBig()
		amount.SetUnscaledBig(ub.Mul(ub, numericSuffix))
	}

	// Cap at min/max bounds.
	sign := amount.Sign()
	if sign == -1 {
		amount.Neg(amount)
	}
	// This rounds non-zero values up to the minimum representable
	// value, under the theory that if you want some resources, you
	// should get some resources, even if you asked for way too small
	// of an amount.
	// Arguably, this should be inf.RoundHalfUp (normal rounding), but
	// that would have the side effect of rounding values < .5m to zero.
	if v, ok := amount.Unscaled(); v != int64(0) || !ok {
		amount.Round(amount, 3, inf.RoundUp)
	}

	// The max is just a simple cap.
	if amount.Cmp(maxAllowed) > 0 {
		amount.Set(maxAllowed)
	}
	if format == BinarySI && amount.Cmp(decOne) < 0 && amount.Cmp(decZero) > 0 {
		// This avoids rounding and hopefully confusion, too.
		format = DecimalSI
	}
	if sign == -1 {
		amount.Neg(amount)
	}

	return &Quantity{amount, format}, nil
}
开发者ID:glerchundi,项目名称:kube2nginx,代码行数:57,代码来源:quantity.go

示例3: Unmarshal

// Reset implements the protobuf marshalling interface.
func (q *Quantity) Unmarshal(data []byte) error {
	p := QuantityProto{}
	if err := p.Unmarshal(data); err != nil {
		return err
	}
	q.Format = p.Format
	b := big.NewInt(0)
	b.SetBytes(p.Bigint)
	q.Amount = inf.NewDecBig(b, inf.Scale(p.Scale))
	return nil
}
开发者ID:thed00de,项目名称:hypernetes,代码行数:12,代码来源:quantity_proto.go

示例4: unmarshalDecimal

func unmarshalDecimal(info TypeInfo, data []byte, value interface{}) error {
	switch v := value.(type) {
	case Unmarshaler:
		return v.UnmarshalCQL(info, data)
	case *inf.Dec:
		scale := decInt(data[0:4])
		unscaled := decBigInt2C(data[4:], nil)
		*v = *inf.NewDecBig(unscaled, inf.Scale(scale))
		return nil
	}
	return unmarshalErrorf("can not unmarshal %s into %T", info, value)
}
开发者ID:RobinUS2,项目名称:gocql,代码行数:12,代码来源:marshal.go

示例5: unmarshalDecimal

func unmarshalDecimal(info *TypeInfo, data []byte, value interface{}) error {
	switch v := value.(type) {
	case Unmarshaler:
		return v.UnmarshalCQL(info, data)
	case **inf.Dec:
		if len(data) > 4 {
			scale := binary.BigEndian.Uint32(data[0:4])
			unscaled := new(big.Int).SetBytes(data[4:])
			*v = inf.NewDecBig(unscaled, inf.Scale(scale))
		}
		return nil
	}
	return unmarshalErrorf("can not unmarshal %s into %T", info, value)
}
开发者ID:qtabot,项目名称:gocql,代码行数:14,代码来源:marshal.go

示例6: unmarshalDecimal

func unmarshalDecimal(info *TypeInfo, data []byte, value interface{}) error {
	switch v := value.(type) {
	case Unmarshaler:
		return v.UnmarshalCQL(info, data)
	case **inf.Dec:
		if len(data) > 4 {
			scale := decInt(data[0:4])
			unscaled := decBigInt2C(data[4:])
			*v = inf.NewDecBig(unscaled, inf.Scale(scale))
			return nil
		} else if len(data) == 0 {
			*v = nil
			return nil
		} else {
			return unmarshalErrorf("can not unmarshal %s into %T", info, value)
		}
	}
	return unmarshalErrorf("can not unmarshal %s into %T", info, value)
}
开发者ID:Gibheer,项目名称:gocql,代码行数:19,代码来源:marshal.go

示例7: dec

func dec(i int64, exponent int) *inf.Dec {
	// See the below test-- scale is the negative of an exponent.
	return inf.NewDec(i, inf.Scale(-exponent))
}
开发者ID:johndmulhausen,项目名称:kubernetes,代码行数:4,代码来源:quantity_test.go

示例8: TestJSON

				q.Amount.SetScale(0)
				q.Amount.SetUnscaled(c.Int63())
				return
			}
			// Be sure to test cases like 1Mi
			q.Amount.SetScale(0)
			q.Amount.SetUnscaled(c.Int63n(1024) << uint(10*c.Intn(5)))
			return
		}
		if c.RandBool() {
			q.Format = DecimalSI
		} else {
			q.Format = DecimalExponent
		}
		if c.RandBool() {
			q.Amount.SetScale(inf.Scale(c.Intn(4)))
			q.Amount.SetUnscaled(c.Int63())
			return
		}
		// Be sure to test cases like 1M
		q.Amount.SetScale(inf.Scale(3 - c.Intn(15)))
		q.Amount.SetUnscaled(c.Int63n(1000))
	},
)

func TestJSON(t *testing.T) {
	for i := 0; i < 500; i++ {
		q := &Quantity{}
		fuzzer.Fuzz(q)
		b, err := json.Marshal(q)
		if err != nil {
开发者ID:johndmulhausen,项目名称:kubernetes,代码行数:31,代码来源:quantity_test.go

示例9: infScale

// infScale adapts a Scale value to an inf.Scale value.
func (s Scale) infScale() inf.Scale {
	return inf.Scale(-s) // inf.Scale is upside-down
}
开发者ID:hongchaodeng,项目名称:c-tutorial,代码行数:4,代码来源:quantity.go


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