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


Golang Rat.SetString方法代码示例

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


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

示例1: PutString

func (me *StatisticalAccumulator) PutString(x string) {

	xx := new(big.Rat)
	xx.SetString(x)

	me.PutRat(xx)
}
开发者ID:reiver,项目名称:go-statisticalaccumulator,代码行数:7,代码来源:statisticalaccumulator_put.go

示例2: UnaryExp

// Unary evaluate unary expression
// include () expression
func UnaryExp(lex *Lex) (*big.Rat, error) {
	if lex.Token == '(' {
		lex.NextToken()
		x, err := AddSubExp(lex)
		if err != nil {
			return nil, err
		}
		if lex.Token != ')' {
			return nil, lex.Error("')' expected but not found. exit.")
		}
		lex.NextToken()
		return x, nil
	}
	if lex.Token != scanner.Int && lex.Token != scanner.Float {
		return nil, lex.Error("number expected. Given token is not number.")
	}

	// found if string can represent as rational.
	var r big.Rat
	rat, ok := r.SetString(lex.Scanner.TokenText())
	if !ok {
		return nil, lex.Error("invalid number")
	}
	lex.NextToken()
	return rat, nil
}
开发者ID:suzuken,项目名称:calc,代码行数:28,代码来源:lex.go

示例3: MakeConst

// MakeConst makes an ideal constant from a literal
// token and the corresponding literal string.
func MakeConst(tok token.Token, lit string) Const {
	switch tok {
	case token.INT:
		var x big.Int
		_, ok := x.SetString(lit, 0)
		assert(ok)
		return Const{&x}
	case token.FLOAT:
		var y big.Rat
		_, ok := y.SetString(lit)
		assert(ok)
		return Const{&y}
	case token.IMAG:
		assert(lit[len(lit)-1] == 'i')
		var im big.Rat
		_, ok := im.SetString(lit[0 : len(lit)-1])
		assert(ok)
		return Const{cmplx{big.NewRat(0, 1), &im}}
	case token.CHAR:
		assert(lit[0] == '\'' && lit[len(lit)-1] == '\'')
		code, _, _, err := strconv.UnquoteChar(lit[1:len(lit)-1], '\'')
		assert(err == nil)
		return Const{big.NewInt(int64(code))}
	case token.STRING:
		s, err := strconv.Unquote(lit)
		assert(err == nil)
		return Const{s}
	}
	panic("unreachable")
}
开发者ID:anuvazhayil,项目名称:HelloWorld_32bitOS,代码行数:32,代码来源:const.go

示例4: TestAmountJSON

func TestAmountJSON(t *testing.T) {
	type item struct {
		Price *Amount
	}
	const expected = `{"Price":"367/100"}`
	p := new(big.Rat)
	p.SetString("367/100")
	x := &item{Price: (*Amount)(p)}

	s, err := json.Marshal(x)
	if err != nil {
		t.Fatal(err)
	}
	if string(s) != expected {
		t.Errorf("got %q, expected %q", string(s), expected)
	}

	x = new(item)
	err = json.Unmarshal([]byte(expected), x)
	if err != nil {
		t.Fatal(err)
	}
	if str := x.Price.String(); str != "3.67" {
		t.Errorf("got %s, expected %s", str, "3.67")
	}
}
开发者ID:remyoudompheng,项目名称:gocash,代码行数:26,代码来源:types_test.go

示例5: fromDbDecimal

func fromDbDecimal(v string) Decimal {
	if v == "" {
		panic("Unexpected empty string value in fromDbDecimal!")
	}
	d := new(big.Rat)
	d.SetString(v)
	return Decimal{Value: d}
}
开发者ID:JamesDunne,项目名称:StockWatcher,代码行数:8,代码来源:util.go

示例6: ToNullDecimal

func ToNullDecimal(v string) NullDecimal {
	if v == "" {
		return NullDecimal{Valid: false}
	}
	r := new(big.Rat)
	r.SetString(v)
	return NullDecimal{Value: r, Valid: true}
}
开发者ID:JamesDunne,项目名称:StockWatcher,代码行数:8,代码来源:types.go

示例7: GetBigAmount

func GetBigAmount(amt string) (*big.Rat, error) {
	var r big.Rat
	_, success := r.SetString(amt)
	if !success {
		return nil, errors.New("Couldn't convert string amount to big.Rat via SetString()")
	}
	return &r, nil
}
开发者ID:aclindsa,项目名称:moneygo,代码行数:8,代码来源:transactions.go

示例8: 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)
}
开发者ID:raff,项目名称:pdfreader,代码行数:9,代码来源:strm.go

示例9: TestPutRat

func TestPutRat(t *testing.T) {

	testsOfTests := [][]struct {
		x                string
		expectedN        string
		expectedMean     string
		expectedVariance string
	}{
		{
			{"5/3", "1/1", "5/3", "0/1"},
			{"5/3", "2/1", "5/3", "0/1"},
			{"5/3", "3/1", "5/3", "0/1"},
			{"5/3", "4/1", "5/3", "0/1"},
		},

		{
			{"4/7", "1/1", "4/7", "0/1"},
		},

		{
			{"1", "1/1", "1/1", "0/1"},
			{"2/1", "2/1", "3/2", "1/4"},
			{"3.0", "3/1", "2/1", "2/3"},
			{"8/2", "4/1", "5/2", "5/4"},
			{"5", "5/1", "3/1", "2/1"},
		},
	}

	for testSetNumber, tests := range testsOfTests {

		statisticalAccumulator := New()

		for datumNumber, datum := range tests {

			x := new(big.Rat)
			x.SetString(datum.x)

			statisticalAccumulator.PutRat(x)

			if actualN := statisticalAccumulator.N(); actualN.String() != datum.expectedN {
				t.Errorf("With test set number [%v], when putting [%v] (i.e., datum number [%v]) into the statistical accumulator, expected N to be [%v], but instead got [%v].", testSetNumber, datum.x, datumNumber, datum.expectedN, actualN)
				continue
			}

			if actualMean := statisticalAccumulator.Mean(); actualMean.String() != datum.expectedMean {
				t.Errorf("With test set number [%v], when putting [%v] (i.e., datum number [%v]) into the statistical accumulator, expected Mean to be [%v], but instead got [%v].", testSetNumber, datum.x, datumNumber, datum.expectedMean, actualMean)
				continue
			}

			if actualVariance := statisticalAccumulator.Variance(); actualVariance.String() != datum.expectedVariance {
				t.Errorf("With test set number [%v], when putting [%v] (i.e., datum number [%v]) into the statistical accumulator, expected Variance to be [%v], but instead got [%v].", testSetNumber, datum.x, datumNumber, datum.expectedVariance, actualVariance)
				continue
			}
		}
	}

}
开发者ID:reiver,项目名称:go-statisticalaccumulator,代码行数:57,代码来源:statisticalaccumulator_put_test.go

示例10: getBalance

func getBalance(balance string) (bool, *big.Rat) {
	rationalNum := new(big.Rat)
	if strings.Contains(balance, "(") {
		rationalNum.SetFloat64(calc.Solve(balance))
		return true, rationalNum
	}
	_, isValid := rationalNum.SetString(balance)
	return isValid, rationalNum
}
开发者ID:howeyc,项目名称:ledger,代码行数:9,代码来源:parse.go

示例11: parseAmount

// Parse the value from the arguments
func parseAmount(text string) (*big.Rat, error) {
	amount := new(big.Rat)

	if r, _ := amount.SetString(text); r != nil {
		return amount, nil
	}

	return nil, errors.New("No value found")
}
开发者ID:revolvingcow,项目名称:cash,代码行数:10,代码来源:helpers.go

示例12: Parse

// Parse takes a string and constructs a Decimal from the string
func Parse(s string) (Decimal, error) {
	rat := new(big.Rat)
	_, success := rat.SetString(s)
	if success {
		return Decimal{true, *rat}, nil
	} else if s == "Inf" || s == "Infinity" {
		// Return a decimal of 1 but note not finite
		return Decimal{false, *big.NewRat(1, 1)}, nil
	} else if s == "-Inf" || s == "-Infinity" {
		// Return a decimal of -1 but note not finite
		return Decimal{false, *big.NewRat(-1, 1)}, nil
	}
	// Return Decimal of 0 but note note finite and error
	return Decimal{false, *big.NewRat(0, 1)}, errors.New("Cannot create Decimal '" + s + "'")
}
开发者ID:raymondjacobson,项目名称:mgo,代码行数:16,代码来源:decimal.go

示例13: Parse

func Parse(v string) (xdr.Int64, error) {
	var f, o, r big.Rat

	_, ok := f.SetString(v)
	if !ok {
		return xdr.Int64(0), fmt.Errorf("cannot parse amount: %s", v)
	}

	o.SetInt64(One)
	r.Mul(&f, &o)

	is := r.FloatString(0)
	i, err := strconv.ParseInt(is, 10, 64)
	if err != nil {
		return xdr.Int64(0), err
	}
	return xdr.Int64(i), nil
}
开发者ID:zenododobird,项目名称:horizon,代码行数:18,代码来源:main.go

示例14: main

//"Pell Equation"
func main() {
	starttime := time.Now()

	total := 0
	for rad := 2; rad <= 100; rad++ {

		if !isSquare(rad) {

			convergentList := make([]int, 1)

			test := frac{a: 0, b: 1, R: rad, d: 1}
			n := 0

			n, test = nextFrac(test)

			convergentList[0] = n

			for i := 0; i < 200; i++ {
				n, test = nextFrac(test)
				convergentList = append(convergentList, n)

			}

			fLength := len(convergentList) - 1

			p, q := ctdFrac(convergentList[:fLength])

			var r big.Rat

			r.SetString(p + "/" + q)

			deciString := r.FloatString(105) //A bit too long in order to avoid rounding

			total += sumDigits(deciString[:101]) //The extra 1 is the decimal point
		}
	}

	fmt.Println(total)

	fmt.Println("Elapsed time:", time.Since(starttime))

}
开发者ID:rif,项目名称:golang-stuff,代码行数:43,代码来源:Problem080.go

示例15: m_

// measure via string
//func m_(s string) Measure {
func m_(s string) float64 {
	r := new(big.Rat)
	_, ok := r.SetString(s)
	if !ok {
		panic("can't convert to measure")
	}

	x := new(big.Rat)

	// d := big.NewRat(1, 256)
	d := big.NewRat(256, 1)

	x.Mul(r, d)

	f, _ := x.Float64()

	//return Measure(int(f))
	return f

}
开发者ID:charneykaye,项目名称:supergollider,代码行数:22,代码来源:measure.go


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