本文整理汇总了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
}
示例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
}
示例3: AddBigFloat
func AddBigFloat() (calculated *big.Float) {
bigFloatStartFloat := big.NewFloat(StartFloat)
bigFloatFactor := big.NewFloat(Factor)
calculated = bigFloatStartFloat.Add(bigFloatStartFloat, bigFloatFactor)
return
}
示例4: MultiplyBigFloat
func MultiplyBigFloat() (calculated *big.Float) {
bigFloatStartFloat := big.NewFloat(StartFloat)
bigFloatFactor := big.NewFloat(Factor)
calculated = bigFloatStartFloat.Mul(bigFloatStartFloat, bigFloatFactor)
return
}
示例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)
}
}
示例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 !!!")
}
}
示例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
}
}
示例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
}
示例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)
}
}
示例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)
}
}
示例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)
}
示例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)
}
})
}
}
示例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
}
示例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
}
示例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)
}