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


Golang big.NewFloat函数代码示例

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


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

示例1: estimateMean

// Estimates the Mean based on the data set
// If the data set is relatively small (< 1000 examples), then remove 1 from the total
func (ad *AnomalyDetection) estimateMean() *big.Float {

	// initialize the total to zero
	totalMean := big.NewFloat(0)

	mean := big.NewFloat(0)

	// Loop thorugh the data set
	for _, element := range ad.dataSet {

		// sum up its elements
		totalMean.Add(totalMean, &element)
		//e, _ := element.Float64()

	}

	// make a copy of the total sum and assign it to the anomaly detection object
	ad.totalSum.Copy(totalMean)

	// calculate the mean
	mean.Quo(totalMean, &ad.totalSamples)

	// assign the mean to the anomaly detection object
	ad.mean = *mean

	return mean
}
开发者ID:sec51,项目名称:goanomaly,代码行数:29,代码来源:gaussian.go

示例2: UpdateServerHardware

// PUT /servers/{server_id}/hardware
func (api *API) UpdateServerHardware(server_id string, hardware *Hardware) (*Server, error) {
	var vc, cpp *int
	var ram *float32
	if hardware.Vcores > 0 {
		vc = new(int)
		*vc = hardware.Vcores
	}
	if hardware.CoresPerProcessor > 0 {
		cpp = new(int)
		*cpp = hardware.CoresPerProcessor
	}
	if big.NewFloat(float64(hardware.Ram)).Cmp(big.NewFloat(0)) != 0 {
		ram = new(float32)
		*ram = hardware.Ram
	}
	req := struct {
		VCores *int     `json:"vcore,omitempty"`
		Cpp    *int     `json:"cores_per_processor,omitempty"`
		Ram    *float32 `json:"ram,omitempty"`
		Flavor string   `json:"fixed_instance_size_id,omitempty"`
	}{VCores: vc, Cpp: cpp, Ram: ram, Flavor: hardware.FixedInsSizeId}

	result := new(Server)
	url := createUrl(api, serverPathSegment, server_id, "hardware")
	err := api.Client.Put(url, &req, &result, http.StatusAccepted)
	if err != nil {
		return nil, err
	}
	result.api = api
	result.decodeRaws()
	return result, nil
}
开发者ID:StackPointCloud,项目名称:oneandone-cloudserver-sdk-go,代码行数:33,代码来源:servers.go

示例3: AddBigFloat

func AddBigFloat() (calculated *big.Float) {
	bigFloatStartFloat := big.NewFloat(StartFloat)
	bigFloatFactor := big.NewFloat(Factor)

	calculated = bigFloatStartFloat.Add(bigFloatStartFloat, bigFloatFactor)

	return
}
开发者ID:Zumata,项目名称:benchmark,代码行数:8,代码来源:decimal.go

示例4: MultiplyBigFloat

func MultiplyBigFloat() (calculated *big.Float) {
	bigFloatStartFloat := big.NewFloat(StartFloat)
	bigFloatFactor := big.NewFloat(Factor)

	calculated = bigFloatStartFloat.Mul(bigFloatStartFloat, bigFloatFactor)

	return
}
开发者ID:Zumata,项目名称:benchmark,代码行数:8,代码来源:decimal.go

示例5: BenchmarkAddBigFloatWithoutNew

func BenchmarkAddBigFloatWithoutNew(b *testing.B) {
	bigFloatStartFloat := big.NewFloat(StartFloat)
	bigFloatFactor := big.NewFloat(Factor)

	for n := 0; n < b.N; n++ {
		AddBigFloatWithoutNew(bigFloatStartFloat, bigFloatFactor)
	}
}
开发者ID:Zumata,项目名称:benchmark,代码行数:8,代码来源:decimal_test.go

示例6: TestSmallDataSet

func TestSmallDataSet(t *testing.T) {

	dataSet := fakeSmallData()

	ad := NewAnomalyDetection(dataSet...)

	totalSamples, _ := ad.totalSamples.Uint64()
	if totalSamples != 20 {
		t.Fatal("Wrong number of element in the set. There are for sure 20, instead we got:", totalSamples)
	}

	totalSum, _ := ad.totalSum.Uint64()
	if totalSum != 83 {
		t.Fatal("Total sum does not add up, should be 83, we got:", totalSum)
	}

	mean, _ := ad.mean.Float64()
	if mean != 4.15 {
		t.Fatal("Mean is wrong, should be 4.15, we got:", mean)
	}

	deviation, _ := ad.deviation.Float64()
	if deviation != 1.3199999999999996 {
		t.Error("Deviation is wrong, should be 1.32 (or 1.3199999999999996), we got:", deviation)
	}

	variance, _ := ad.variance.Float64()
	if variance != 2.3325000000000005 {
		t.Error("Variance is wrong, should be 2.3325 (or 2.3325000000000005), we got:", variance)
	}

	anomaly, result := ad.EventIsAnomalous(*big.NewFloat(4.3), big.NewFloat(0.02))
	if anomaly {
		t.Errorf("5.3 should not be an anomaly !!! %f\n", result)
	}

	// stricter threshold
	anomaly, _ = ad.EventIsAnomalous(*big.NewFloat(7.3), big.NewFloat(0.1))
	if !anomaly {
		t.Error("7.3 should be an anomaly !!!")
	}

	ad.ExpandDataSet(fakeSmallData()...)
	ad.ExpandDataSet(fakeSmallData()...)

	anomaly, _ = ad.EventIsAnomalous(*big.NewFloat(5.3), big.NewFloat(0.01))
	if anomaly {
		t.Error("5.3 should not be an anomaly !!!")
	}

	// stricter threshold
	anomaly, _ = ad.EventIsAnomalous(*big.NewFloat(7.3), big.NewFloat(0.1))
	if !anomaly {
		t.Error("7.3 should be an anomaly !!!")
	}

}
开发者ID:sec51,项目名称:goanomaly,代码行数:57,代码来源:gaussian_test.go

示例7: BigFloatGen

func BigFloatGen(r Int64F) Generator {
	return func() interface{} {
		a := float64(r())
		b := float64(r()) + 1.0
		x := big.NewFloat(a)
		y := big.NewFloat(b)
		y = y.Quo(x, y)
		return y
	}
}
开发者ID:russolsen,项目名称:ohyeah,代码行数:10,代码来源:generator.go

示例8: splitRangeString

func splitRangeString(start, end string, splits int) []string {
	results := []string{start}
	if start == end {
		return results
	}
	if end < start {
		tmp := start
		start = end
		end = tmp
	}

	// find longest common prefix between strings
	minLen := len(start)
	if len(end) < minLen {
		minLen = len(end)
	}
	prefix := ""
	for i := 0; i < minLen; i++ {
		if start[i] == end[i] {
			prefix = start[0 : i+1]
		} else {
			break
		}
	}

	// remove prefix from strings to split
	start = start[len(prefix):]
	end = end[len(prefix):]

	ordStart := stringToOrd(start)
	ordEnd := stringToOrd(end)

	tmp := new(big.Int)
	tmp.Sub(ordEnd, ordStart)

	stride := new(big.Float)
	stride.SetInt(tmp)
	stride.Quo(stride, big.NewFloat(float64(splits)))

	for i := 1; i <= splits; i++ {
		tmp := new(big.Float)
		tmp.Mul(stride, big.NewFloat(float64(i)))
		tmp.Add(tmp, new(big.Float).SetInt(ordStart))

		result, _ := tmp.Int(new(big.Int))

		value := prefix + ordToString(result, 0)

		if value != results[len(results)-1] {
			results = append(results, value)
		}
	}

	return results
}
开发者ID:CaptainCodeman,项目名称:datastore-mapper,代码行数:55,代码来源:range_splitter.go

示例9: TestRoot

func TestRoot(t *testing.T) {
	x := big.NewFloat(0.12381245613960218386)
	n := 16
	res := Root(x, n)
	exp := big.NewFloat(0.8776023372475015)
	diff := new(big.Float).Sub(res, exp)
	diff = diff.Abs(diff)
	if diff.Cmp(big.NewFloat(0.00000001)) >= 0 {
		log.Fatal("Exp failed:", exp, res)
	}
}
开发者ID:kwonalbert,项目名称:spacemint,代码行数:11,代码来源:util_test.go

示例10: TestPow

func TestPow(t *testing.T) {
	x := big.NewFloat(0.12381245613960218386)
	n := 3
	res := Pow(x, n)
	exp := big.NewFloat(0.00189798605)
	diff := new(big.Float).Sub(res, exp)
	diff = diff.Abs(diff)
	if diff.Cmp(big.NewFloat(0.00000001)) >= 0 {
		log.Fatal("Pow failed:", exp, res)
	}
}
开发者ID:kwonalbert,项目名称:spacemint,代码行数:11,代码来源:util_test.go

示例11: Sqrt

// Sqrt returns a big.Float representation of the square root of
// z. Precision is the same as the one of the argument. The function
// panics if z is negative, returns ±0 when z = ±0, and +Inf when z =
// +Inf.
func Sqrt(z *big.Float) *big.Float {

	// panic on negative z
	if z.Sign() == -1 {
		panic("Sqrt: argument is negative")
	}

	// √±0 = ±0
	if z.Sign() == 0 {
		return big.NewFloat(float64(z.Sign()))
	}

	// √+Inf  = +Inf
	if z.IsInf() {
		return big.NewFloat(math.Inf(+1))
	}

	// Compute √(a·2**b) as
	//   √(a)·2**b/2       if b is even
	//   √(2a)·2**b/2      if b > 0 is odd
	//   √(0.5a)·2**b/2    if b < 0 is odd
	//
	// The difference in the odd exponent case is due to the fact that
	// exp/2 is rounded in different directions when exp is negative.
	mant := new(big.Float)
	exp := z.MantExp(mant)
	switch exp % 2 {
	case 1:
		mant.Mul(big.NewFloat(2), mant)
	case -1:
		mant.Mul(big.NewFloat(0.5), mant)
	}

	// Solving x² - z = 0 directly requires a Quo call, but it's
	// faster for small precisions.
	//
	// Solving 1/x² - z = 0 avoids the Quo call and is much faster for
	// high precisions.
	//
	// Use sqrtDirect for prec <= 128 and sqrtInverse for prec > 128.
	var x *big.Float
	if z.Prec() <= 128 {
		x = sqrtDirect(mant)
	} else {
		x = sqrtInverse(mant)
	}

	// re-attach the exponent and return
	return x.SetMantExp(x, exp/2)

}
开发者ID:ALTree,项目名称:floats,代码行数:55,代码来源:sqrt.go

示例12: BenchmarkLog

func BenchmarkLog(b *testing.B) {
	z := big.NewFloat(2).SetPrec(1e5)
	_ = bigfloat.Log(z) // fill pi cache before benchmarking

	for _, prec := range []uint{1e2, 1e3, 1e4, 1e5} {
		z = big.NewFloat(2).SetPrec(prec)
		b.Run(fmt.Sprintf("%v", prec), func(b *testing.B) {
			b.ReportAllocs()
			for n := 0; n < b.N; n++ {
				bigfloat.Log(z)
			}
		})
	}
}
开发者ID:ALTree,项目名称:floats,代码行数:14,代码来源:log_test.go

示例13: mandelbrot

func mandelbrot(z complex128) color.Color {
	const iterations = 200
	const contrast = 15

	var v complex128
	limit := new(big.Float).SetFloat64(2.0)
	for n := uint8(0); n < iterations; n++ {
		v = v*v + z
		if hypot(big.NewFloat(real(v)), big.NewFloat(imag(v))).Cmp(limit) > 2 {
			return color.Gray{255 - contrast*n}
		}
	}
	return color.Black
}
开发者ID:suzuken,项目名称:gopl,代码行数:14,代码来源:bigfloat.go

示例14: mandelbrotFloat

func mandelbrotFloat(r, i *big.Float) color.Color {
	const iterations = 200
	const contrast = 15

	a := big.NewFloat(0) //実部の初期値
	b := big.NewFloat(0) //虚部の初期値
	for n := uint8(0); n < iterations; n++ {
		a = addF(subF(mulF(a, a), mulF(b, b)), r)
		b = addF(mulF(big.NewFloat(2), mulF(a, b)), i)
		if addF(mulF(a, a), mulF(b, b)).Cmp(big.NewFloat(4)) == 1 {
			return color.Gray{255 - contrast*n}
		}
	}
	return color.Black
}
开发者ID:zDpxq6,项目名称:go,代码行数:15,代码来源:main.go

示例15: String

func (m *meanagg) String() string {
	if m.d == 0 {
		return "NaN"
	}
	v := new(big.Float).Quo(m.v, big.NewFloat(m.d))
	return v.Text('f', -1)
}
开发者ID:robinjha,项目名称:clair,代码行数:7,代码来源:num.go


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